c# - .net WebSocket: CloseOutputAsync vs CloseAsync -
We have a working ASP.NET Web API REST service, which is one of the methods of our controller, HTTPTTEX. .)
Socket handler code looks something like this ...
Public async task socket handler (AspNetWebSocketContext context) {_webSocket = context.WebSocket; ... while (! Cts.IsCancellationRequested) {WebSocketReceiveResult Results = _webSocket.ReceiveAsync (Input Segment, cts.Token) .Result; WebSocketsStateCollusocketState = _webSocket.State; If (result.MessageType == WebSocketMessageType.Close || currentSocketState == WebSocketState.CloseReceived) {// What should I use. CloseAysnc () or. CloseOutputAsync ()? _webSocket.CloseOutputAsync (WebSocketCloseStatus.NormalClosure, "Client Requested", cts.Token). Wait (); } If (currentSocketState == WebSocketState.Open) {...}}}
.What is the difference between .CooseAsync () and CloseOutputAysnc ()? I tried both of them and they both seemed to work fine but some difference should be the same they both describe very similar to MSDN ...
System.Net.WebSokcets .closeAsync - Closes the WebSocket Connection as an Asynchronous operation by using a handshake defined in WebSketts Protocol specification Section 7
System.Net.WebSockets.CloseOutputAsync - WebSocket Protocol specification initiates or completes a handshake defined in section 7.
When sent a message to the connected party, and waits for a receipt. ... The second option is to use CloseOutputAsync this is more of a "fire-and-forget" approach ...
It seems that you are receiving a closed message;
If (the result message type == WebSocketMessageType.Close || currentSocketState == WebSocketState.CloseReceived)
So I would say that you only Using CloseOutputAsync
would be OK because you have already received a message that says, "I want to turn off this connection".
Comments
Post a Comment