unity使用socket實現(xiàn)聊天室功能
更新時間:2021年03月11日 08:48:57 作者:ai上白菜
這篇文章主要為大家詳細(xì)介紹了unity使用socket實現(xiàn)聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了unity使用socket實現(xiàn)聊天室的具體代碼,供大家參考,具體內(nèi)容如下
unity聊天室服務(wù)端實現(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ù)器已開啟...");
while (true)
{
Socket clientSocket = tcpServer.Accept();
Client client = new Client(clientSocket);
//client.ReceiveMessage();
clientLists.Add(client);
}
}
}
}
服務(wù)端Client類
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客戶端實現(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));
//客戶端開啟線程接收數(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)行時會執(zhí)行
* */
void OnDestroy()
{
//關(guān)閉連接,分接收功能和發(fā)送功能,both為兩者均關(guān)閉
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
}



以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c# 9.0新特性nint和Pattern matching的使用方法
這篇文章主要介紹了c# 9.0新特性nint和Pattern matching的使用方法,文中講解非常細(xì)致,幫助你更好的學(xué)習(xí)c# 9.0,有需求的朋友可以參考下2020-06-06
C# 使用Dictionary復(fù)制克隆副本及比較是否相等
這篇文章主要介紹了C# 使用Dictionary復(fù)制克隆副本及比較是否相等,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
C#程序中使用LINQ to XML來查詢XML格式數(shù)據(jù)的實例
這篇文章主要介紹了C#程序中使用LINQ to XML來查詢XML格式數(shù)據(jù)的實例,LINQ to XML是.NET框架中集成的接口,可以將XML數(shù)據(jù)放到內(nèi)存中進(jìn)行處理,需要的朋友可以參考下2016-03-03

