您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Unity如何實(shí)現(xiàn)聊天室功能的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
服務(wù)器需要有以下幾個(gè)步驟
1、確定Socket協(xié)議類型(采用TCP協(xié)議或者UDP協(xié)議)
2、綁定服務(wù)器的IP地址和端口號(hào)
3、設(shè)置最大監(jiān)聽數(shù)量
4、等到連接并處理消息
由于服務(wù)器屬于一對(duì)多的處理關(guān)系,因?yàn)槲覀冃枰镁€程來(lái)監(jiān)聽消息:
class Client { private Socket clientSocket; private Thread t; public bool Connected { get { return clientSocket.Connected; } } private byte[] data = new byte[1024];//數(shù)據(jù)容器 public Client(Socket client) { clientSocket = client; //啟動(dòng)一個(gè)線程,處理客戶端的接受 t = new Thread(ReceiveMsg); t.Start(); } private void ReceiveMsg() { while (true) { //在接收數(shù)據(jù)之前,判斷Socket連接是否斷開 if (!clientSocket.Connected) { clientSocket.Close(); break;//跳出循環(huán)終止線程的執(zhí)行 } int length=clientSocket.Receive(data); string msg = Encoding.UTF8.GetString(data, 0, length); //服務(wù)端接收數(shù)據(jù)后,要將數(shù)據(jù)分發(fā)到客戶端 //廣播消息 Program.BroadcastMsg(msg); } } public void SendMsg(string msg) { byte[] data = Encoding.UTF8.GetBytes(msg); clientSocket.Send(data); } }
服務(wù)器主要代碼:
class Program { static List<Client> clients = new List<Client>(); //本機(jī)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客戶端代碼就十分簡(jiǎn)單,監(jiān)聽服務(wù)器的消息并顯示到界面上即可
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中不允許在線程中調(diào)用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)); //創(chuàng)建一個(gè)線程用來(lái)接收消息 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(); } }
實(shí)際運(yùn)行效果(注意需要先啟動(dòng)服務(wù)器):
感謝各位的閱讀!關(guān)于“Unity如何實(shí)現(xiàn)聊天室功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。