Files
plugin-library/Assets/00.StaryEvo/Runtime/UnitySocket/SocketClient/Scripts/SocketServerMain.cs
2025-09-04 11:43:35 +08:00

41 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Text;
using UnityEngine;
public class SocketServerMain : MonoBehaviour
{
private SocketServer _server;
private void Awake()
{
_server = new SocketServer("192.168.31.67", 6854);
_server.OnReceive += (client, 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;
}
};
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
// 踢出连接
foreach (var item in _server.ClientInfoDic.Keys)
_server.KickOutAll();
}
private void OnDestroy()
{
// 注意由于Unity编译器环境下游戏开启/关闭只影响主线程的开关游戏关闭回调时需要通过Close函数来关闭服务端/客户端的线程。
if (_server != null) _server.Close();
}
}