|
|
@@ -107,13 +107,13 @@ namespace YSAI.Core.communication.net.ws.service
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- HttpListenerContext httpListenerContext = Communication.GetContextAsync().Result;
|
|
|
+ HttpListenerContext httpListenerContext = Communication.GetContextAsync().WaitAsync(token.Token).Result;
|
|
|
if (httpListenerContext.Request.IsWebSocketRequest) // 如果是websocket请求
|
|
|
{
|
|
|
- string IpPort = httpListenerContext.Request.RemoteEndPoint.Address.ToString();
|
|
|
+ string IpPort = httpListenerContext.Request.RemoteEndPoint.ToString();
|
|
|
try
|
|
|
{
|
|
|
- WebSocketContext webSocketContext = httpListenerContext.AcceptWebSocketAsync(subProtocol: null).Result;
|
|
|
+ WebSocketContext webSocketContext = httpListenerContext.AcceptWebSocketAsync(subProtocol: null).WaitAsync(token.Token).Result;
|
|
|
if (!ClientIoc.ContainsKey(IpPort)) //当字典中没有这个连接对象
|
|
|
{
|
|
|
CancellationTokenSource token = new CancellationTokenSource();
|
|
|
@@ -121,7 +121,7 @@ namespace YSAI.Core.communication.net.ws.service
|
|
|
{
|
|
|
WebSocketObj = webSocketContext,
|
|
|
Switch = token,
|
|
|
- TaskObj = MonitorMessageTask(token, webSocketContext)
|
|
|
+ TaskObj = MonitorMessageTask(token, webSocketContext.WebSocket,IpPort)
|
|
|
};
|
|
|
//把此对象添加到字典中
|
|
|
ClientIoc.AddOrUpdate(IpPort, client, (k, v) => client);
|
|
|
@@ -144,7 +144,6 @@ namespace YSAI.Core.communication.net.ws.service
|
|
|
{
|
|
|
OnEventHandler(this, new EventResult(false, $"[{ClassName}]监控客户端连接异常:{ex.Message}"));
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}, token.Token);
|
|
|
}
|
|
|
@@ -153,19 +152,20 @@ namespace YSAI.Core.communication.net.ws.service
|
|
|
/// 监控消息任务
|
|
|
/// </summary>
|
|
|
/// <param name="token"></param>
|
|
|
+ /// <param name="webSocket"></param>
|
|
|
+ /// <param name="IpPort"></param>
|
|
|
/// <returns></returns>
|
|
|
- private Task MonitorMessageTask(CancellationTokenSource token, WebSocketContext webSocket)
|
|
|
+ private Task MonitorMessageTask(CancellationTokenSource token, WebSocket webSocket,string IpPort)
|
|
|
{
|
|
|
//起一个新线程来监控
|
|
|
return Task.Factory.StartNew(() =>
|
|
|
{
|
|
|
- string IpPort = $"{webSocket.RequestUri.Authority}:{webSocket.RequestUri.Port}";
|
|
|
- ArraySegment<byte> DataByte = new ArraySegment<byte>(); //数据缓冲区
|
|
|
+ ArraySegment<byte> DataByte = new ArraySegment<byte>(new byte[1024*1024]); //数据缓冲区
|
|
|
while (!token.IsCancellationRequested)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- WebSocketReceiveResult result = webSocket.WebSocket.ReceiveAsync(DataByte, token.Token).Result; // 接收数据,并返回数据的长度;
|
|
|
+ WebSocketReceiveResult result = webSocket.ReceiveAsync(DataByte, token.Token).Result; // 接收数据,并返回数据的长度;
|
|
|
if (result.Count > 0)
|
|
|
{
|
|
|
OnEventHandler(this, new EventResult(true, $"[{IpPort}]数据接收成功", new WsServiceData.ClientMessage { Step = WsServiceData.Steps.消息接收, IpPort = IpPort, Data = DataByte.Array }, @enum.ResultType.Bytes));
|
|
|
@@ -175,6 +175,8 @@ namespace YSAI.Core.communication.net.ws.service
|
|
|
OnEventHandler(this, new EventResult(false, $"[{IpPort}]发来的数据长度错误(小于等于零),已强制关闭连接", new WsServiceData.ClientMessage { Step = WsServiceData.Steps.客户端断开, IpPort = IpPort }, @enum.ResultType.All));
|
|
|
//移除此客户端
|
|
|
RemoveAsync(IpPort);
|
|
|
+ //强制退出此线程
|
|
|
+ return;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
@@ -182,6 +184,7 @@ namespace YSAI.Core.communication.net.ws.service
|
|
|
OnEventHandler(this, new EventResult(false, $"监控[{IpPort}]消息异常:{ex.Message}", new WsServiceData.ClientMessage { Step = WsServiceData.Steps.客户端断开, IpPort = IpPort }, @enum.ResultType.All));
|
|
|
//移除此客户端
|
|
|
RemoveAsync(IpPort);
|
|
|
+ return;
|
|
|
}
|
|
|
}
|
|
|
}, token.Token);
|
|
|
@@ -197,6 +200,12 @@ namespace YSAI.Core.communication.net.ws.service
|
|
|
{
|
|
|
return Break("On", false, "已打开");
|
|
|
}
|
|
|
+ Communication = new HttpListener();
|
|
|
+ foreach (var localHost in basics.LocalHostArray)
|
|
|
+ {
|
|
|
+ Communication.Prefixes.Add(localHost);
|
|
|
+ }
|
|
|
+ Communication.Start();
|
|
|
//监控开关
|
|
|
if (MonitorConnectSwitch == null || MonitorConnectSwitch.IsCancellationRequested)
|
|
|
{
|
|
|
@@ -242,7 +251,7 @@ namespace YSAI.Core.communication.net.ws.service
|
|
|
{
|
|
|
item.Value.Switch.Cancel();
|
|
|
item.Value.TaskObj.Wait();
|
|
|
- item.Value.WebSocketObj.WebSocket.CloseAsync(WebSocketCloseStatus.Empty,"手动关闭",CancellationToken.None);
|
|
|
+ item.Value.WebSocketObj.WebSocket.CloseAsync(WebSocketCloseStatus.Empty,null,CancellationToken.None);
|
|
|
item.Value.WebSocketObj.WebSocket.Abort();
|
|
|
item.Value.WebSocketObj.WebSocket.Dispose();
|
|
|
}
|
|
|
@@ -281,10 +290,10 @@ namespace YSAI.Core.communication.net.ws.service
|
|
|
{
|
|
|
ClientIoc[IpPort].Switch.Cancel();
|
|
|
ClientIoc[IpPort].TaskObj.Wait();
|
|
|
- ClientIoc[IpPort].WebSocketObj.WebSocket.CloseAsync(WebSocketCloseStatus.Empty, "客户端连接异常", CancellationToken.None);
|
|
|
+ ClientIoc[IpPort].WebSocketObj.WebSocket.CloseAsync(WebSocketCloseStatus.Empty,null, CancellationToken.None);
|
|
|
ClientIoc[IpPort].WebSocketObj.WebSocket.Abort();
|
|
|
ClientIoc[IpPort].WebSocketObj.WebSocket.Dispose();
|
|
|
-
|
|
|
+ ClientIoc.Remove(IpPort, out _);
|
|
|
return Break("Remove", true);
|
|
|
}
|
|
|
return Break("Remove", false, "Ip Port 不存在");
|
|
|
@@ -300,7 +309,7 @@ namespace YSAI.Core.communication.net.ws.service
|
|
|
/// <returns></returns>
|
|
|
public Task<OperateResult> RemoveAsync(string? Key)
|
|
|
{
|
|
|
- return Task.Run(() => RemoveAsync(Key));
|
|
|
+ return Task.Run(() => Remove(Key));
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 数据发送
|
|
|
@@ -313,48 +322,59 @@ namespace YSAI.Core.communication.net.ws.service
|
|
|
Depart("Send");
|
|
|
try
|
|
|
{
|
|
|
- List<string> Message = new List<string>();
|
|
|
- if (string.IsNullOrEmpty(IpPort))
|
|
|
+ if (Communication != null)
|
|
|
{
|
|
|
- //群发
|
|
|
- foreach (var client in ClientIoc)
|
|
|
+ List<string> Message = new List<string>();
|
|
|
+ if (string.IsNullOrEmpty(IpPort))
|
|
|
{
|
|
|
- string IP = $"{client.Value.WebSocketObj.RequestUri.Authority}:{client.Value.WebSocketObj.RequestUri.Port}";
|
|
|
- try
|
|
|
+ //群发
|
|
|
+ foreach (var client in ClientIoc)
|
|
|
{
|
|
|
- client.Value.WebSocketObj.WebSocket.SendAsync(new ArraySegment<byte>(Data), WebSocketMessageType.Text, true, CancellationToken.None);
|
|
|
+ if (ClientIoc.Count.Equals(0))
|
|
|
+ {
|
|
|
+ return Break("Send", false, "客户端未连接");
|
|
|
+ }
|
|
|
+ string IP = client.Key;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ client.Value.WebSocketObj.WebSocket.SendAsync(new ArraySegment<byte>(Data), WebSocketMessageType.Text, true, CancellationToken.None);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Message.Add($"[{IP}]数据发送异常:{ex.Message}");
|
|
|
+ }
|
|
|
}
|
|
|
- catch (Exception ex)
|
|
|
+
|
|
|
+ if (Message.Count > 0)
|
|
|
{
|
|
|
- Message.Add($"[{IP}]数据发送异常:{ex.Message}");
|
|
|
+ return Break("Send", false, "存在失败数据", Message, @enum.ResultType.All);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- if (Message.Count > 0)
|
|
|
- {
|
|
|
- return Break("Send", false, "存在失败数据", Message, @enum.ResultType.All);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- //指定发送
|
|
|
- if (ClientIoc.ContainsKey(IpPort))
|
|
|
+ else
|
|
|
{
|
|
|
- try
|
|
|
+ //指定发送
|
|
|
+ if (ClientIoc.ContainsKey(IpPort))
|
|
|
{
|
|
|
- ClientIoc[IpPort].WebSocketObj.WebSocket.SendAsync(new ArraySegment<byte>(Data), WebSocketMessageType.Text, true, CancellationToken.None);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ClientIoc[IpPort].WebSocketObj.WebSocket.SendAsync(new ArraySegment<byte>(Data), WebSocketMessageType.Text, true, CancellationToken.None);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ return Break("Send", false, $"数据发送[{IpPort}]异常:{ex.Message}", Message, @enum.ResultType.All);
|
|
|
+ }
|
|
|
}
|
|
|
- catch (Exception ex)
|
|
|
+ else
|
|
|
{
|
|
|
- return Break("Send", false, $"数据发送[{IpPort}]异常:{ex.Message}", Message, @enum.ResultType.All);
|
|
|
+ return Break("Send", false, $"数据发送失败,[{IpPort}]不存在", Message, @enum.ResultType.All);
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- return Break("Send", false, $"数据发送失败,[{IpPort}]不存在", Message, @enum.ResultType.All);
|
|
|
- }
|
|
|
+ return Break("Send", true);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return Break("Send", false, $"未启动");
|
|
|
}
|
|
|
- return Break("Send", true);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|