11111
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using Stary.Evo;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Main
|
||||
{
|
||||
public interface IClientSocketSystem : ISystem
|
||||
{
|
||||
/// <summary>
|
||||
/// 连接
|
||||
/// </summary>
|
||||
void OnConnect(string ip);
|
||||
|
||||
/// <summary>
|
||||
/// 向服务端发送消息
|
||||
/// </summary>
|
||||
/// <param name="type">消息类型</param>
|
||||
/// <param name="message">消息内容</param>
|
||||
void Send(SocketEvent type, string message);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 断开连接
|
||||
/// </summary>
|
||||
void DisConnect();
|
||||
}
|
||||
|
||||
|
||||
public class ClientSocketSystem : AbstractSystem, IClientSocketSystem
|
||||
{
|
||||
private SocketClient _socketClient;
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
// 注意由于Unity编译器环境下,游戏开启/关闭只影响主线程的开关,游戏关闭回调时需要通过Close函数来关闭服务端/客户端的线程。
|
||||
if (_socketClient != null) _socketClient.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向服务端发送消息
|
||||
/// </summary>
|
||||
/// <param name="type">消息类型</param>
|
||||
/// <param name="message">消息内容</param>
|
||||
public void Send(SocketEvent type, string message)
|
||||
{
|
||||
var bytes = Encoding.UTF8.GetBytes(message);
|
||||
_socketClient.Send((ushort)type, bytes);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 退出服务端
|
||||
/// </summary>
|
||||
public void DisConnect()
|
||||
{
|
||||
// 踢出连接
|
||||
_socketClient.DisConnect();
|
||||
}
|
||||
|
||||
public void OnConnect(string ip)
|
||||
{
|
||||
_socketClient = new SocketClient(ip, 10000);
|
||||
|
||||
|
||||
_socketClient.Connect(() =>
|
||||
{
|
||||
Debug.Log("连接成功");
|
||||
_socketClient.OnReceive += OnReceive;
|
||||
// _client.DisConnect();
|
||||
}, () => { Debug.Log("连接失败"); });
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnInit()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 接收客户端消息
|
||||
/// </summary>
|
||||
/// <param name="client">指定客户端</param>
|
||||
/// <param name="data">消息数据</param>
|
||||
private void OnReceive(SocketDataPack data)
|
||||
{
|
||||
Debug.LogFormat("接收到数据>>>{0} {1}", (SocketEvent)data.Type, data.Buff.Length);
|
||||
|
||||
switch ((SocketEvent)data.Type)
|
||||
{
|
||||
case SocketEvent.ServerMessage:
|
||||
Debug.LogFormat("接收到服务器数据 >>> {0}", Encoding.UTF8.GetString(data.Data));
|
||||
var message = Encoding.UTF8.GetString(data.Data);
|
||||
|
||||
break;
|
||||
case SocketEvent.ClientDisconn:
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public string GetLocalIPv4()
|
||||
{
|
||||
return Dns.GetHostEntry(Dns.GetHostName())
|
||||
.AddressList.First(f => f.AddressFamily == AddressFamily.InterNetwork)
|
||||
.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cf683938375f4ecf8f186b6f27cc1090
|
||||
timeCreated: 1753945542
|
||||
@@ -0,0 +1,165 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 83370f66e6954f62a28b9e38c266819a
|
||||
timeCreated: 1753940317
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 588ac15fc35e4262979673d60d5360d5
|
||||
timeCreated: 1753941532
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e1043c3356e14496b3692c245bfb461b
|
||||
timeCreated: 1753941548
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace Server
|
||||
{
|
||||
public enum ServerOpenSceneSchemaType
|
||||
{
|
||||
BirdView, //鸟瞰
|
||||
Spot //临场
|
||||
}
|
||||
|
||||
public enum ServerOpenSceneType
|
||||
{
|
||||
Unit1, //场景1
|
||||
Unit2, //场景2
|
||||
Unit3, //场景3
|
||||
Unit4 //场景4
|
||||
}
|
||||
|
||||
public enum ServerPanelType
|
||||
{
|
||||
OpenScene, //打开场景
|
||||
ControlSchema, //控制场景开关
|
||||
QueryClient //查询客户端
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 信号强度
|
||||
/// </summary>
|
||||
public enum RSSI
|
||||
{
|
||||
Good,
|
||||
Bad,
|
||||
Not
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cab61756e91e4736afde89c57340b821
|
||||
timeCreated: 1753941563
|
||||
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
[Serializable]
|
||||
public struct ResultCodeEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// 状态码ServerPanelType
|
||||
/// </summary>
|
||||
private string code { get; set; }
|
||||
|
||||
private object message { get; set; }
|
||||
|
||||
private ResultCodeEntity(string code, object message)
|
||||
{
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
|
||||
public string Code()
|
||||
{
|
||||
return code;
|
||||
}
|
||||
|
||||
public object Message()
|
||||
{
|
||||
return message;
|
||||
}
|
||||
|
||||
public static readonly ResultCodeEntity OpenScene = new(ServerPanelType.OpenScene.ToString(), "");
|
||||
public static readonly ResultCodeEntity ControlSchema = new(ServerPanelType.ControlSchema.ToString(), "");
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public struct ResultEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// 状态码ServerPanelType
|
||||
/// </summary>
|
||||
public string code { get; set; }
|
||||
|
||||
public string deviceCode { get; set; }
|
||||
|
||||
public object message { get; set; }
|
||||
|
||||
public void SetDeviceCode(string deviceCode)
|
||||
{
|
||||
this.deviceCode = deviceCode;
|
||||
}
|
||||
|
||||
public string DeviceCode()
|
||||
{
|
||||
return deviceCode;
|
||||
}
|
||||
|
||||
public void SetMessage(object message)
|
||||
{
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
|
||||
public ResultEntity(string code, string deviceCode, object message)
|
||||
{
|
||||
this.code = code;
|
||||
this.deviceCode = deviceCode;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public ResultEntity(ResultCodeEntity resultCode, string deviceCode)
|
||||
{
|
||||
code = resultCode.Code();
|
||||
this.deviceCode = deviceCode;
|
||||
message = resultCode.Message();
|
||||
}
|
||||
|
||||
public static string ToEntityString(ResultEntity entity)
|
||||
{
|
||||
var data = JsonConvert.SerializeObject(entity, Formatting.Indented);
|
||||
return JsonConvert.SerializeObject(data);
|
||||
}
|
||||
|
||||
public static ResultEntity SetResultCode(ResultCodeEntity resultCode, string deviceCode)
|
||||
{
|
||||
var result = new ResultEntity(resultCode, deviceCode);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ResultEntity SetResultCode(ResultCodeEntity resultCode, object message)
|
||||
{
|
||||
var result = new ResultEntity(resultCode, "");
|
||||
result.message = message;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ResultEntity SetResultCode(ResultCodeEntity resultCode, string deviceCode, object message)
|
||||
{
|
||||
var result = new ResultEntity(resultCode, deviceCode);
|
||||
result.message = message;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 41f092961d2c4f33a0d0e980158dd89a
|
||||
timeCreated: 1753941907
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace Server
|
||||
{
|
||||
public struct ServerOpenSceneEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// ServerOpenSceneType
|
||||
/// </summary>
|
||||
public string serverOpenSceneType;
|
||||
|
||||
/// <summary>
|
||||
/// ServerOpenSceneSchemaType
|
||||
/// </summary>
|
||||
public string serverOpenSceneSchemaType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fbb30584a7ca4dd7afadf941cb67692e
|
||||
timeCreated: 1753941679
|
||||
Reference in New Issue
Block a user