using System.Net; using System.Net.Sockets; using System.Text; using Newtonsoft.Json; using Stary.Evo; using UnityEngine; namespace Server { public interface IServerSocketSystem : ISystem { /// /// 连接服务器 /// /// void OnConnect(string ip = ""); /// /// 向指定客户端发送消息 /// /// 指定客户端 /// 消息类型 /// 消息内容 void Send(Socket client, SocketEvent type, string message); /// /// 向所有客户端发送消息 /// /// 消息类型 /// 消息内容 void SendAllClient(SocketEvent type, ResultEntity message); /// /// 踢出指定客户端 /// /// 指定客户端 void KickOut(Socket client); /// /// 踢出所有客户端 /// void KickOutAll(); } public class ServerSocketSystem : AbstractSystem, IServerSocketSystem { private SocketServer _socketServer; public override void Dispose() { // 注意由于Unity编译器环境下,游戏开启/关闭只影响主线程的开关,游戏关闭回调时需要通过Close函数来关闭服务端/客户端的线程。 if (_socketServer != null) _socketServer.Close(); } /// /// 向指定客户端发送消息 /// /// 指定客户端 /// 消息类型 /// 消息内容 public void Send(Socket client, SocketEvent type, string message) { var bytes = Encoding.UTF8.GetBytes(message); _socketServer.Send(client, (ushort)type, bytes); } /// /// 向所有客户端发送消息 /// /// 消息类型 /// 消息内容 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); } } /// /// 服务器踢出客户端 /// public void KickOut(Socket client) { // 踢出连接 _socketServer.KickOut(client); } /// /// 服务器踢出客户端 /// 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() { } /// /// 接收客户端消息 /// /// 指定客户端 /// 消息数据 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; } } } }