欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

unity使用socket实现聊天室功能

时间:2021-11-13 15:30:10|栏目:.NET代码|点击:

本文实例为大家分享了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 OnSendButtonClick()
 {
 string value = MessageInput.text;
 SendMessage(value);
 MessageInput.text = " ";
 }
 /**
 * unity自带方法
 * 停止运行时会执行
 * */
 void OnDestroy()
 {
 //关闭连接,分接收功能和发送功能,both为两者均关闭
 clientSocket.Shutdown(SocketShutdown.Both);
 clientSocket.Close();
 }
}

上一篇:unity实现简单抽奖系统

栏    目:.NET代码

下一篇:Unity3D实现简易五子棋源码

本文标题:unity使用socket实现聊天室功能

本文地址:http://www.codeinn.net/misctech/183407.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有