UDP实现一个简易的聊天室 (Unity&&C#完成)
  效果展示(尚未完善)

UDP
User Data Protocol 用户数据报协议
概述
UDP是不连接的数据报模式。即传输数据之前源端和终端不建立连接。使用尽最大努力交付原则,即不保证可靠交付。
数据报模式:由于不建立连接,收到的数据可能是任意主机发送的,所以接收端Read次数必须与发送端Write次数相同,每次只接收一个报文,避免多个报文合并。但如果报文过长,多出部分会被丢弃,所以注意数据最大为1472字节。
实现步骤
服务端 客户端
获取本机终结点 获取本机终结点
创建UdpClient对象 创建UdpClient对象
接收任意终结点消息 接收任意终结点消息
向客户端发送消息 向服务端发送消息
…… …
关闭连接 关闭连接
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Common;
using UIWidgetsSamples;
using System; /// <summary>
/// 服务端
/// </summary>
public class ChatUDPServerTest : MonoBehaviour
{
public string serverIP;
//IP地址
public int serverPort;
//端口
//1.创建Scoket对象 IP Port
private Thread thread;
private UdpClient udpSeivic;
public void Start()
{
chatView = transform.FindChildByName("ChatView").
GetComponent<ChatView>();
//给端口和IP
//构建终结点 IP和一个端口
IPEndPoint localEP = new
IPEndPoint(IPAddress.Parse(serverIP), serverPort);
udpSeivic = new UdpClient(localEP); thread = new Thread(ReceiveMessage);
thread.Start();
} /// <summary>
/// 接收消息
/// </summary>
private void ReceiveMessage()
{
while (true)
{
IPEndPoint remote = new
IPEndPoint(IPAddress.Any, );
//创建任意终结点
//ref
byte[] date = udpSeivic.Receive(ref remote);
//Receive接收消息 如果没有收到消息 线程阻塞 放在线程中
string msg = Encoding.UTF8.GetString(date);
//获取的客户都安信息
Debug.Log(remote.Address + "===" + remote.Port);
//如果接收客户端的消息,会把任意终结点修改为客户端的终结点
ThreadCrossHelper.Instance.ExecuteOnMainThread(() => { ShowMessage(msg); });
}
}
private ChatView chatView;
/// <summary>
/// 显示消息
/// </summary>
/// <param name="msg"></param>
public void ShowMessage(string msg)
{
chatView.DataSource.Add(new ChatLine()
{
UserName = "AnnnS",
Message = msg,
Time = DateTime.Now,
Type = ChatLineType.User,
});
}
private void OnApplicationQuit()
{
udpSeivic.Close();
thread.Abort();
}
}
ChatUDPServerTest
脚本引用的工具箱
- MonoSingleton (泛型单例)
- ThreadCrossHelper (为子线程提供,可以在主线程中执行的方法)
- TransformHelper(根据名称查找后代元素)
using System.Collections;
using System.Collections.Generic;
using UnityEngine; namespace Common
{
/// <summary>
///
/// </summary>
public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
//public static T Instance
//{
// get;
// private set;
//}
//private void Awake()
//{
// Instance = this as T;
//}
//按需加载
private static T instance;
public static T Instance
{
get
{
if (instance == null)
{
//在场景中查找对象
instance = FindObjectOfType<T>();
if (instance == null)
{
//创建游戏对象 附加 脚本对象
new GameObject("Singleton of " + typeof(T)).AddComponent<T>();//立即执行Awake
}
else
{
instance.Initialized();
}
}
return instance;
}
} protected virtual void Initialized()
{ } [Tooltip("是否需要跨场景不销毁")]
public bool isDontDestroy = true; //如果管理类自行附加到物体中
//在Awake中为instance赋值
protected void Awake()
{
if (isDontDestroy)
{
DontDestroyOnLoad(gameObject);
}
if (instance == null)
{
instance = this as T;
instance.Initialized();
}
}
}
}
MonoSingleton
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine; namespace Common
{
/// <summary>
///
/// </summary>
public class ThreadCrossHelper : MonoSingleton<ThreadCrossHelper>
{
/// <summary>
/// 延迟项
/// </summary>
class DelayedItem
{
public Action CurrentAction { get; set; }
public DateTime Time { get; set; }
} private List<DelayedItem> actionList;
//private List<Action> actionList;
//private List<float> timeList; protected override void Initialized()
{
base.Initialized(); actionList = new List<DelayedItem>();
} private void Update()
{
for (int i = actionList.Count - ; i >= ; i--)
{
//到时间
if (actionList[i].Time <= DateTime.Now)
{
lock (actionList)
{
actionList[i].CurrentAction();//执行
actionList.RemoveAt(i);//从列表中移除
}
}
}
}
/// <summary>
/// 为子线程提供,可以在主线程中执行的方法
/// </summary>
/// <param name="action"></param>
/// <param name="dealy"></param>
public void ExecuteOnMainThread(Action action, float dealy = )
{
DelayedItem item = new DelayedItem()
{
CurrentAction = action,
//Time = Time.time + dealy
Time = DateTime.Now.AddSeconds(dealy)
};
lock (actionList)
{
actionList.Add(item);
}
}
}
}
ThreadCrossHelper
using System.Collections;
using System.Collections.Generic;
using UnityEngine; namespace Common
{
/// <summary>
/// 变换组件助手类
/// </summary>
public static class TransformHelper
{
/// <summary>
/// 未知层级,根据名称查找后代元素
/// </summary>
/// <param name="currentTF"></param>
/// <param name="childName"></param>
/// <returns></returns>
public static Transform FindChildByName(this Transform currentTF, string childName)
{
Transform childTF = currentTF.Find(childName);
if (childTF != null) return childTF;
//将问题推迟给子物体
for (int i = ; i < currentTF.childCount; i++)
{
//在方法体内部,又遇到了相同的问题,所以需要调用自身。
childTF = FindChildByName(currentTF.GetChild(i), childName);
if (childTF != null) return childTF;
}
return null;
}
}
}
TransformHelper
UDP实现一个简易的聊天室 (Unity&&C#完成)的更多相关文章
- C 基于UDP实现一个简易的聊天室
		引言 本文是围绕Linux udp api 构建一个简易的多人聊天室.重点看思路,帮助我们加深 对udp开发中一些api了解.相对而言udp socket开发相比tcp socket开发注意的细节要少 ... 
- TCP实现一个简易的聊天室 (Unity&&C#完成)
		效果展示 TCP Transmission Control Protocol 传输控制协议 TCP是面向连接的流模式(俗称:网络流).即传输数据之前源端和终端建立可靠的连接,保证数据传输的正确性. 流 ... 
- 与众不同 windows phone (31) - Communication(通信)之基于 Socket UDP 开发一个多人聊天室
		原文:与众不同 windows phone (31) - Communication(通信)之基于 Socket UDP 开发一个多人聊天室 [索引页][源码下载] 与众不同 windows phon ... 
- 与众不同 windows phone (30) - Communication(通信)之基于 Socket TCP 开发一个多人聊天室
		原文:与众不同 windows phone (30) - Communication(通信)之基于 Socket TCP 开发一个多人聊天室 [索引页][源码下载] 与众不同 windows phon ... 
- [SignalR]一个简单的聊天室
		原文:[SignalR]一个简单的聊天室 1.说明 开发环境:Microsoft Visual Studio 2010 以及需要安装NuGet. 2.添加SignalR所需要的类库以及脚本文件: 3. ... 
- 用ServletContext做一个简单的聊天室
		这里主要是ServletContext的一个特性:ServletContext是一个公共的空间,可以被所有的客户访问.由此可见ServletContext比cookie和session的作用范围要大[ ... 
- 基于websocket实现的一个简单的聊天室
		本文是基于websocket写的一个简单的聊天室的例子,可以实现简单的群聊和私聊.是基于websocket的注解方式编写的.(有一个小的缺陷,如果用户名是中文,会乱码,不知如何处理,如有人知道,请告知 ... 
- [XMPP]简易的聊天室实现[二](使用CocoaAsyncSocket)
		@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ... 
- 利用JavaUDPSocket+多线程模拟实现一个简单的聊天室程序
		对Socket的一点个人理解:Socket原意是指插座.家家户户都有五花八门的家用电器,但它们共用统一制式的插座.这样做的好处就是将所有家用电器的通电方式统一化,不需要大费周章地在墙壁上凿洞并专门接电 ... 
随机推荐
- 给iOS开发新手送点福利,简述UIButton的属性和用法
			UIButton属性 1.UIButton状态: UIControlStateNormal // 正常状态 UIControlStateHighlighted // 高 ... 
- 3_bootsrap布局容器
			3.布局容器 BootStrap必须需要至少一个布局容器,才能为页面内容进行封装和方便的样式控制. 相当于一个画板. 帮助手册位置:全局CSS样式------->概览------->布局容 ... 
- vs2010 出现“未能将 ProteusDebugEngine 调试器附加到计算机”
			vs2010 打开项目时出现如下图错误,解决方法: 1.查看C:\Progream Files下的Internet Explorer文件夹还在不在,不在则会出现此问题: 2.可以右键项目属性-调试-勾 ... 
- maven学习系列  之  常见问题
			1.新建的maven项目无法修改 Project Facets 的 Dynamic Web Module 版本 RE: 在工程目录下有一个.settings文件夹,打开org.eclipse.wst. ... 
- Yii中利用filters来控制访问
			filters()方法定义在CController里,用Gii生成Controller时里面就有filters方法,代码如下: public function filters() { // retur ... 
- JVM  调优参数设置
			先看Linux内存大小(假设为2G) cat /proc/meminfo |grep MemTotal 查看java初始配置 java -XX:+PrintFlagsInitial Tomcat配置 ... 
- UNITY polygon collider不随物体旋转
			U3D中的一般包围框如 boxcollider, meshcollider, capsule collider等都会随物体旋转而旋转.然而polygon collider却不会. 补充:原来所有2D包 ... 
- java基础三 [深入多态,接口和多态](阅读Head First Java记录)
			抽象类和抽象方法 1.抽象类的声明方法,在前面加上抽象类的关键词abstract abstract class canine extends animal{ public void roam ... 
- 第七章 二叉搜索树 (a)概述
- java工具类 --千分位方法
			/** * 千分位方法 * @param text * @return */ public static String fmtMicrometer(String text) { DecimalForm ... 
