更改了远程环境下websocket会来回发送重复消息的问题

This commit is contained in:
fengjiayi
2024-10-28 10:25:57 +08:00
parent e2f1ec5810
commit f20cfb755c
20 changed files with 297 additions and 167 deletions

View File

@@ -234,7 +234,10 @@ namespace Serein.Library.Network.WebSocketCommunication
MsgQueueUtil msgQueueUtil,
WebSocketAuthorizedHelper authorizedHelper)
{
async Task sendasync(string text)
{
await SocketExtension.SendAsync(webSocket, text); // 回复客户端,处理方法中入参如果需要发送消息委托,则将该回调方法作为委托参数传入
}
while (true)
{
var message = await msgQueueUtil.WaitMsgAsync(); // 有消息时通知
@@ -251,16 +254,16 @@ namespace Serein.Library.Network.WebSocketCommunication
return;
}
}
_ = Task.Run(() => {
JObject json = JObject.Parse(message);
WebSocketMsgContext context = new WebSocketMsgContext(async (text) =>
{
await SocketExtension.SendAsync(webSocket, text); // 回复客户端,处理方法中入参如果需要发送消息委托,则将该回调方法作为委托参数传入
});
context.JsonObject = json;
MsgHandleHelper.HandleMsg(context); // 处理消息
});
using (var context = new WebSocketMsgContext(sendasync))
{
context.JsonObject = JObject.Parse(message);
await MsgHandleHelper.HandleAsync(context); // 处理消息
}
//_ = Task.Run(() => {
//});
}