这篇文章主要为大家详细介绍了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实现聊天室功能
基础教程推荐
猜你喜欢
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- 创建属性设置器委托 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01