我是靠谱客的博主 灵巧枕头,最近开发中收集的这篇文章主要介绍unity使用socket实现聊天室功能,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

本文实例为大家分享了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使用socket实现聊天室功能的全部内容,希望文章能够帮你解决unity使用socket实现聊天室功能所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部