unity使用socket实现聊天室功能

编辑: admin 分类: c#语言 发布时间: 2021-11-25 来源:互联网

本文实例为大家分享了unity使用socket实现聊天室的具体代码,供大家参考,具体内容如下

unity聊天室服务端实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
namespace 服务端_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("服务器已开启...");
  while (true)
  {
  Socket clientSocket = tcpServer.Accept();
  Client client = new Client(clientSocket);
  //client.ReceiveMessage();
  clientLists.Add(client);
  }
 }
 }

}

服务端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 服务端_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客户端实现

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];// 数据容器
 private string message = "";
 void Start()
 {
 ConnectToServer();
 }


 void Update()
 {
 //只有在主线程才能更新UI
 if (message != "" && message != null)
 {
  MessageText.text += "\n" + message;
  message = "";
 }
 }
 /**
 * 连接服务器端函数
 * */
 void ConnectToServer()
 {
 clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 //跟服务器连接
 clientSocket.Connect(new IPEndPoint(IPAddress.Parse(ipaddress), port));
 //客户端开启线程接收数据
 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 OnSendButto【来源:美国站群服务器 请说明出处】nClick()
 {
 string value = MessageInput.text;
 SendMessage(value);
 MessageInput.text = " ";
 }
 /**
 * unity自带方法
 * 停止运行时会执行
 * */
 void OnDestroy()
 {
 //关闭连接,分接收功能和发送功能,both为两者均关闭
 clientSocket.Shutdown(SocketShutdown.Both);
 clientSocket.Close();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。

【文章出处:防御服务器