Files
plugin-library/Assets/00.StaryEvo/Runtime/UnitySocket/SocketClient/Scripts/PackgeSocket/ServerSocketSystem.cs

165 lines
5.5 KiB
C#
Raw Normal View History

2025-09-04 11:43:35 +08:00
using System.Net;
using System.Net.Sockets;
using System.Text;
using Newtonsoft.Json;
using Stary.Evo;
using UnityEngine;
namespace Server
{
public interface IServerSocketSystem : ISystem
{
/// <summary>
/// 连接服务器
/// </summary>
/// <param name="ip"></param>
void OnConnect(string ip = "");
/// <summary>
/// 向指定客户端发送消息
/// </summary>
/// <param name="client">指定客户端</param>
/// <param name="type">消息类型</param>
/// <param name="message">消息内容</param>
void Send(Socket client, SocketEvent type, string message);
/// <summary>
/// 向所有客户端发送消息
/// </summary>
/// <param name="type">消息类型</param>
/// <param name="message">消息内容</param>
void SendAllClient(SocketEvent type, ResultEntity message);
/// <summary>
/// 踢出指定客户端
/// </summary>
/// <param name="client">指定客户端</param>
void KickOut(Socket client);
/// <summary>
/// 踢出所有客户端
/// </summary>
void KickOutAll();
}
public class ServerSocketSystem : AbstractSystem, IServerSocketSystem
{
private SocketServer _socketServer;
public override void Dispose()
{
// 注意由于Unity编译器环境下游戏开启/关闭只影响主线程的开关游戏关闭回调时需要通过Close函数来关闭服务端/客户端的线程。
if (_socketServer != null) _socketServer.Close();
}
/// <summary>
/// 向指定客户端发送消息
/// </summary>
/// <param name="client">指定客户端</param>
/// <param name="type">消息类型</param>
/// <param name="message">消息内容</param>
public void Send(Socket client, SocketEvent type, string message)
{
var bytes = Encoding.UTF8.GetBytes(message);
_socketServer.Send(client, (ushort)type, bytes);
}
/// <summary>
/// 向所有客户端发送消息
/// </summary>
/// <param name="type">消息类型</param>
/// <param name="message">消息内容</param>
public void SendAllClient(SocketEvent type, ResultEntity message)
{
Debug.Log("SendAllClient向客户端发送消息" + JsonConvert.SerializeObject(message));
foreach (var item in _socketServer.ClientInfoDic)
{
message.SetDeviceCode(item.Value.IP);
Debug.Log("向客户端发送消息:" + ResultEntity.ToEntityString(message));
var bytes = Encoding.UTF8.GetBytes(ResultEntity.ToEntityString(message));
_socketServer.Send(item.Key, (ushort)type, bytes);
}
}
/// <summary>
/// 服务器踢出客户端
/// </summary>
public void KickOut(Socket client)
{
// 踢出连接
_socketServer.KickOut(client);
}
/// <summary>
/// 服务器踢出客户端
/// </summary>
public void KickOutAll()
{
// 踢出连接
foreach (var item in _socketServer.ClientInfoDic.Keys)
_socketServer.KickOutAll();
}
public void OnConnect(string ip = "")
{
if (string.IsNullOrEmpty(ip))
ip = "0.0.0.0";
_socketServer = new SocketServer(ip, 10000);
_socketServer.OnReceive += OnReceive;
_socketServer.OnConnect += OnConnect;
_socketServer.OnDisconnect += OnDisconnect;
_socketServer.OnHeadTimeOut += OnHeadTimeOut;
}
public void OnConnect(Socket client)
{
var endPoint = client.RemoteEndPoint as IPEndPoint;
var clientIp = endPoint?.Address.ToString() ?? "未知IP";
this.SendEvent(ServerPanelType.QueryClient,
clientIp, RSSI.Good);
}
private void OnDisconnect(Socket client)
{
var endPoint = client.RemoteEndPoint as IPEndPoint;
var clientIp = endPoint?.Address.ToString() ?? "未知IP";
this.SendEvent(ServerPanelType.QueryClient,
clientIp, RSSI.Not);
}
private void OnHeadTimeOut(Socket client)
{
var endPoint = client.RemoteEndPoint as IPEndPoint;
var clientIp = endPoint?.Address.ToString() ?? "未知IP";
this.SendEvent(ServerPanelType.QueryClient,
clientIp, RSSI.Bad);
}
protected override void OnInit()
{
}
/// <summary>
/// 接收客户端消息
/// </summary>
/// <param name="client">指定客户端</param>
/// <param name="data">消息数据</param>
private void OnReceive(Socket client, SocketDataPack data)
{
Debug.LogFormat("[{0}]接收到数据>>>{1} {2}", client.LocalEndPoint, (SocketEvent)data.Type, data.Buff.Length);
switch ((SocketEvent)data.Type)
{
case SocketEvent.ScTest:
Debug.LogFormat("接收到测试数据 >>> {0}", Encoding.UTF8.GetString(data.Data));
break;
case SocketEvent.ClientDisconn:
Debug.LogFormat("客户端断开连接 >>> {0}", client.LocalEndPoint);
break;
}
}
}
}