这篇文章给大家分享的是有关Unity如何实现聊天室功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
服务器需要有以下几个步骤
1、确定Socket协议类型(采用TCP协议或者UDP协议)
2、绑定服务器的IP地址和端口号
3、设置最大监听数量
4、等到连接并处理消息
由于服务器属于一对多的处理关系,因为我们需要用线程来监听消息:
class Client { private Socket clientSocket; private Thread t; public bool Connected { get { return clientSocket.Connected; } } private byte[] data = new byte[1024];//数据容器 public Client(Socket client) { clientSocket = client; //启动一个线程,处理客户端的接受 t = new Thread(ReceiveMsg); t.Start(); } private void ReceiveMsg() { while (true) { //在接收数据之前,判断Socket连接是否断开 if (!clientSocket.Connected) { clientSocket.Close(); break;//跳出循环终止线程的执行 } int length=clientSocket.Receive(data); string msg = Encoding.UTF8.GetString(data, 0, length); //服务端接收数据后,要将数据分发到客户端 //广播消息 Program.BroadcastMsg(msg); } } public void SendMsg(string msg) { byte[] data = Encoding.UTF8.GetBytes(msg); clientSocket.Send(data); } }
服务器主要代码:
class Program { static List<Client> clients = new List<Client>(); //本机IP:192.168.100.172 static void Main(string[] args) { Socket tcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); tcpServer.Bind(new IPEndPoint(IPAddress.Parse("192.168.100.172"), 7788)); tcpServer.Listen(5000); Console.WriteLine("Server Running......."); while (true) { var clientSocket = tcpServer.Accept(); Console.WriteLine("建立连接"); Client client = new Client(clientSocket); clients.Add(client); } } public static void BroadcastMsg(string msg) { var noConnecteds = new List<Client>(); foreach (var client in clients) { if (client.Connected) { client.SendMsg(msg); } else { noConnecteds.Add(client); } } foreach (var del in noConnecteds) { clients.Remove(del); } } }
Unity客户端代码
Unity客户端代码就十分简单,监听服务器的消息并显示到界面上即可
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Net; using System.Net.Sockets; using System.Text; using UnityEngine.UI; using System.Threading; public class ChatManager : MonoBehaviour { public string IP = "192.168.100.172"; public int Port = 7788; private Socket client; private Thread t; public InputField input; public Button sendBtn; public Text item; public string name; private string msg=string.Empty; // Start is called before the first frame update void Start() { ConnectedToServer(); sendBtn.onClick.AddListener(() => { SendMsg(input.text); input.text = string.Empty; }); } // Update is called once per frame void Update() { //由于在Unity中不允许在线程中调用UnityAPI,因此需要的Update中刷新显示 if (!string.IsNullOrEmpty(msg)) { item.text += "\n" + msg; msg = string.Empty; } } private void ConnectedToServer() { client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(new IPEndPoint(IPAddress.Parse(IP), Port)); //创建一个线程用来接收消息 t = new Thread(ReceiveMsg); t.Start(); } byte[] data = new byte[1024]; public void ReceiveMsg() { while (true) { if (!client.Connected) { break; } int length = client.Receive(data); msg = Encoding.UTF8.GetString(data, 0, length); } } public void SendMsg(string msg) { byte[] data = Encoding.UTF8.GetBytes(name+":"+msg); client.Send(data); } public void OnDestroy() { client.Close(); } }
实际运行效果(注意需要先启动服务器):
感谢各位的阅读!关于“Unity如何实现聊天室功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。