gpt4 book ai didi

c# - WebSockets SendAsync 异常

转载 作者:行者123 更新时间:2023-11-30 22:07:12 34 4
gpt4 key购买 nike

我正在使用 SendAsync 方法循环发送 1000 条消息,并在这行代码中得到下面的异常

await _ws.SendAsync(new ArraySegment<byte>(messageBuffer, offset, count), WebSocketMessageType.Text, lastMessage, _cancellationToken);

我做错了什么?我知道某处一定是瓶颈,因为我在发送之间没有 sleep 。我可以等待某个状态来证明下面的异常吗?

System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=There is already one outstanding 'SendAsync' call for this WebSocket instance. ReceiveAsync and SendAsync can be called simultaneously, but at most one outstanding operation for each of them is allowed at the same time.
Source=System
StackTrace:
at System.Net.WebSockets.WebSocketBase.<SendAsyncCore>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at zzz.Stub.WebSocketWrapper.<SendMessageAsync>d__0.MoveNext() in zzz

最佳答案

您不能并行发送或接收,请记住这一点。由于您正在等待,所以看起来不错。

如果您有其他线程发送 ping 或保持事件消息,则可能是问题所在。

我使用该库进行负载测试,因此我创建了很多连接并发送了很多消息并且它有效。它不是很好,但很管用。

关于c# - WebSockets SendAsync 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23151605/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com