You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OMS.NET/MapServer.cs

71 lines
3.0 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Net;
using System.Text;
using System.Text.Unicode;
using OMS.NET.Instructs;
using WebSocketSharp;
using WebSocketSharp.Server;
using ErrorEventArgs = WebSocketSharp.ErrorEventArgs;
namespace OMS.NET
{
public class MapServer : WebSocketBehavior
{
private IPEndPoint iPEndPoint = new(IPAddress.Any, 0);
protected override async void OnMessage(MessageEventArgs e)
{
GlobalArea.Log.Debug(this.ID + " " + this.Context.UserEndPoint.ToString() + ":" + e.Data);
Instruct? instruct = Instruct.JsonStringParse(e.Data);
if (instruct != null)
{
await instruct.HandlerAndMeasure(this.ID);//传递ws连接的id为某些需要判断ws连接状态的处理逻辑准备
if (instruct.ResponseOrBroadcastInstructs.Count > 0)
{
instruct.ResponseOrBroadcastInstructs.ForEach(res =>
{
if (res.IsResponse)
{
string str = res.ToJsonString();
Send(str);
//GlobalArea.Log.Debug("已发送回复 " + str);
}
if (res.IsBroadcast)
{
string str = res.ToJsonString();
foreach (IWebSocketSession session in this.Sessions.Sessions)
{
//看起来只有登录后的连接才能收到广播,这里添加下过滤
if (GlobalArea.LoginCheckByID(session.ID))
{
session.Context.WebSocket.Send(str);
}
}
//GlobalArea.Log.Debug("已发送广播 " + str);
}
});
}
}
}
protected override void OnOpen()
{
this.iPEndPoint = this.Context.UserEndPoint;
GlobalArea.AddUserConnect(this.ID, this.iPEndPoint);
Console.WriteLine(this.ID + " " + this.iPEndPoint.ToString() + " Conection Open");
Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}");
}
protected override void OnClose(CloseEventArgs e)
{
GlobalArea.RemoveUserConnectByID(this.ID);
Console.WriteLine(this.ID + " " + this.iPEndPoint.ToString() + " Conection Close" + e.Reason);
Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}");
}
protected override void OnError(ErrorEventArgs e)
{
GlobalArea.RemoveUserConnectByID(this.ID);
Console.WriteLine(this.ID + " " + this.iPEndPoint.ToString() + " Conection Error Close" + e.Message);
Console.WriteLine($"当前连接客户端数量: {GlobalArea.ConnectClientsCount}");
}
}
}