amplifier
amplifier

Reputation: 1833

Proper reconnect after WebSocketException

I use ClientWebSocket to connect to a socket server. If I stop the server I got WebSocketException what is OK. Then I try to reconnect.

If I use

await wsClient.ConnectAsync(serviceUri, cts.Token);

I get "System.InvalidOperationException: 'The WebSocket has already been started.'"

If I try to close it before reconnecting by

await wsClient.CloseAsync(WebSocketCloseStatus.Empty, string.Empty, CancellationToken.None);

I get "System.Net.WebSockets.WebSocketException: 'The WebSocket is in an invalid state ('Aborted') for this operation. Valid states are: 'Open, CloseReceived, CloseSent'' "

So what is the proper way of reconnecting after the connection has been lost.

Upvotes: 4

Views: 3230

Answers (1)

EagleBeak
EagleBeak

Reputation: 7419

I've just run into the same problem. Apparently you cannot reuse an aborted ClientWebSocket. The docs of ClientWebSocket.Abort() say:

The ClientWebSocket cannot be reused once it is aborted.

Upvotes: 1

Related Questions