unity使用socket實(shí)現(xiàn)聊天室功能
本文實(shí)例為大家分享了unity使用socket實(shí)現(xiàn)聊天室的具體代碼,供大家參考,具體內(nèi)容如下
unity聊天室服務(wù)端實(shí)現(xiàn)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.Sockets; using System.Net; namespace 服務(wù)端_03 { class Program { static string ip = "192.168.0.102"; static int port = 7788; static List<Client> clientLists = new List<Client>(); public static void BrocastMessage(string s) { var notConnectLists = new List<Client>(); foreach (var client in clientLists) { if(client.Connected) { client.SendMessage(s); } else { notConnectLists.Add(client); } } foreach (var client in notConnectLists) { clientLists.Remove(client); } } static void Main(string[] args) { Socket tcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint iPEnd = new IPEndPoint(IPAddress.Parse(ip), port); tcpServer.Bind(iPEnd); tcpServer.Listen(100); Console.WriteLine("服務(wù)器已開(kāi)啟..."); while (true) { Socket clientSocket = tcpServer.Accept(); Client client = new Client(clientSocket); //client.ReceiveMessage(); clientLists.Add(client); } } } }
服務(wù)端Client類(lèi)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.Sockets; using System.Threading; namespace 服務(wù)端_03 { class Client { Socket clientSocket; byte[] data = new byte[1024]; Thread t; public Client(Socket clientSocket) { this.clientSocket = clientSocket; t = new Thread(ReceiveMessage); t.Start(); } public void SendMessage(string s) { byte[] data = Encoding.UTF8.GetBytes(s); clientSocket.Send(data); } public void ReceiveMessage() { while (true) { if(clientSocket.Poll(10,SelectMode.SelectRead)) { clientSocket.Close(); break; } int length = clientSocket.Receive(data); string message = Encoding.UTF8.GetString(data, 0, length); //dosomething 向所有的客戶端廣播消息 Program.BrocastMessage(message); Console.WriteLine(message); ; } } public bool Connected { get { return clientSocket.Connected; } } } }
unity客戶端實(shí)現(xiàn)
using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using UnityEngine; using System.Collections; using UnityEngine.UI; public class ChatClient : MonoBehaviour { public string ipaddress = "172.25.14.165"; public int port = 7799; private Socket clientSocket; public InputField MessageInput; public Text MessageText; private Thread thread; private byte[] data = new byte[1024];// 數(shù)據(jù)容器 private string message = ""; void Start() { ConnectToServer(); } void Update() { //只有在主線程才能更新UI if (message != "" && message != null) { MessageText.text += "\n" + message; message = ""; } } /** * 連接服務(wù)器端函數(shù) * */ void ConnectToServer() { clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //跟服務(wù)器連接 clientSocket.Connect(new IPEndPoint(IPAddress.Parse(ipaddress), port)); //客戶端開(kāi)啟線程接收數(shù)據(jù) thread = new Thread(ReceiveMessage); thread.Start(); } void ReceiveMessage() { while (true) { if (clientSocket.Connected == false) { break; } int length = clientSocket.Receive(data); message = Encoding.UTF8.GetString(data, 0, length); print(message); } } new void SendMessage(string message) { byte[] data = Encoding.UTF8.GetBytes(message); clientSocket.Send(data); } public void OnSendButtonClick() { string value = MessageInput.text; SendMessage(value); MessageInput.text = " "; } /** * unity自帶方法 * 停止運(yùn)行時(shí)會(huì)執(zhí)行 * */ void OnDestroy() { //關(guān)閉連接,分接收功能和發(fā)送功能,both為兩者均關(guān)閉 clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c# 9.0新特性nint和Pattern matching的使用方法
這篇文章主要介紹了c# 9.0新特性nint和Pattern matching的使用方法,文中講解非常細(xì)致,幫助你更好的學(xué)習(xí)c# 9.0,有需求的朋友可以參考下2020-06-06C# 使用Dictionary復(fù)制克隆副本及比較是否相等
這篇文章主要介紹了C# 使用Dictionary復(fù)制克隆副本及比較是否相等,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12C#程序中使用LINQ to XML來(lái)查詢XML格式數(shù)據(jù)的實(shí)例
這篇文章主要介紹了C#程序中使用LINQ to XML來(lái)查詢XML格式數(shù)據(jù)的實(shí)例,LINQ to XML是.NET框架中集成的接口,可以將XML數(shù)據(jù)放到內(nèi)存中進(jìn)行處理,需要的朋友可以參考下2016-03-03Silverlight實(shí)現(xiàn)跑馬燈動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Silverlight實(shí)現(xiàn)跑馬燈動(dòng)畫(huà),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07