我使用SendAsync方法在循环中发送1000条消息,并在此行代码中获得以下异常
await _ws.SendAsync(new ArraySegment<byte>(messageBuffer, offset, count), WebSocketMessageType.Text, lastMessage, _cancellationToken);
我做错了什么?我知道某处必然是瓶颈,因为我在发送之间没有睡眠。我可以等待某个州以防止下面的例外吗?
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
答案 0 :(得分:3)
你不能做并行发送或接收,记住这一点。因为你在等待它似乎很好。
如果您有其他线程发送ping或保持活动消息,则可能是问题。
我使用该库进行负载测试,因此我创建了大量连接并发送了大量消息,并且它可以工作。它不是很好,但它确实有效。