您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。