简体   繁体   中英

c# - Handling closing/disconnecting web socket connections (CloseAsync vs CloseOutputAsync)

I am trying to understand and handle the different ways a web socket connection could be closed. From msdn :

You can use the CloseAsync and CloseOutputAsync methods for both client-initiated and server-initiated requests to close an AspNetWebSocket connection. The two methods handle client-initiated requests in the same way: After the client sends a message to the server to close the connection, the server calls one of these methods and sends an acknowledgment to the client, and then the method returns.

For server-initiated requests, the two methods work differently. The CloseAsync method sends a message to the client to close the connection, waits for a response, and then returns. The server does not wait for any additional data sent by the client. In contrast, the CloseOutputAsync method sends a message to the client to close the connection and returns without waiting for a response. After the method returns, you can call the ReceiveAsync method and handle either additional data or the acknowledgment that the client sends.

In my application the client basically sits there receiving updates from the server, but never actually sends anything to the server outside of opening the connection. However the client also needs to be able to close the connection from their side.

Basically the server's role is to sit there waiting to open a connection, send a bunch of stuff, and handle clients closing connections (but never has to decide to close the connection or not).

So no matter, when we are talking about cleanly closing a connection, a client needs to send the server a message saying it wants to close the connection, correct?

So the client will need to do something like:

var x = await closeAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None)

How does the server know the client executed closeAync? I tried to send a piece of data from server to client using:

await sendAsync(new ArraySegment<byte>(infoBytes), 1, false, cancelled);

and received an error:

"The received message type 'Text' is invalid after calling WebSocket.CloseAsync. WebSocket.CloseAsync should only be used if no more data is expected from the remote endpoint. Use 'WebSocket.CloseOutputAsync' instead to keep being able to receive data but close the output channel."

So how on the server side do I know if the client is trying to cleanly close the connection?

On the server side :

 var result = await webSocket.ReceiveAsync(buffer, cancellationToken);

            if (result.MessageType == WebSocketMessageType.Close)
            {
                await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Closed in server by the client", CancellationToken.None);
                return null;
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM