websocket-client
websocket-client copied to clipboard
Handling - The remote party closed the WebSocket connection without completing the close handshake.
I have written some code based on the samples provided. It connects to a websocket for a fixed amount of time and then closes the connection and reopens it.
Every now and then I get the following error;
[ERR] [WEBSOCKET Demo] Error while listening to websocket stream, error: 'The remote party closed the WebSocket connection without completing the close handshake.'
What then happens is that my code continues to wait and only reopens the connection when that time is over.
Below is a simplified version of my code. My question is how can I try and reopen the connection after this error (or wait 10 seconds before reopening it).
namespace Demo
{
class Program
{
private static readonly ManualResetEvent ExitEvent = new ManualResetEvent(false);
public static async Task Cable(int resetInMilliseconds, string host, string subscribtion, string message)
{
do
{
try
{
var factory = new Func<ClientWebSocket>(() =>
{
var client = new ClientWebSocket { Options = { KeepAliveInterval = TimeSpan.FromSeconds(1) } };
return client;
});
using (IWebsocketClient client = new WebsocketClient(Uri(host), factory))
{
client.Name = "Demo";
client.IsReconnectionEnabled = false;
client
.ReconnectionHappened
.Subscribe(type =>
{
Console.WriteLine($"Reconnection happened, type: {type}, url: {client.Url}");
});
client
.DisconnectionHappened
.Subscribe(info =>
{
Console.WriteLine($"Disconnection happened, type: {info.Type}");
});
client
.MessageReceived
.Subscribe(msg =>
{
Console.WriteLine($"Message received: {msg}");
});
await client.Start();
client.Send(subscribtion);
client.Send(message);
ExitEvent.WaitOne(resetInMilliseconds);
}
}
catch (Exception e)
{
Console.WriteLine("Error occured, Error: " + e.Message);
}
} while (true);
}
}
}
Hi @Vinedine ,
it is done automatically in the background.
Set client.IsReconnectionEnabled to true.
If it triggers too many reconnections based on NoMessageReceived, higher timeout in client.ReconnectTimeout docs or set it to null.
Hi,
Thanks for the reply. I included the following in my code but I am not sure it works in all scenarios.
client.IsReconnectionEnabled = true;
client.ReconnectTimeout = new TimeSpan(0, 0, 0, 10);
Below are two extracts from my log file.
In the below it has worked;
07:15:29.561 [ERR] [WEBSOCKET Test] Error while listening to websocket stream, error: 'The remote party closed the WebSocket connection without completing the close handshake.'
System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.
---> System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.
at System.Net.WebSockets.ManagedWebSocket.ThrowIfEOFUnexpected(Boolean throwOnPrematureClosure)
at System.Net.WebSockets.ManagedWebSocket.EnsureBufferContainsAsync(Int32 minimumRequiredBytes, CancellationToken cancellationToken, Boolean throwOnPrematureClosure)
at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TWebSocketReceiveResultGetter,TWebSocketReceiveResult](Memory`1 payloadBuffer, CancellationToken cancellationToken, TWebSocketReceiveResultGetter resultGetter)
at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TWebSocketReceiveResultGetter,TWebSocketReceiveResult](Memory`1 payloadBuffer, CancellationToken cancellationToken, TWebSocketReceiveResultGetter resultGetter)
at Websocket.Client.WebsocketClient.Listen(WebSocket client, CancellationToken token)
07:15:38.910 [INF] Reconnection happened, type: Websocket.Client.Models.ReconnectionInfo, url: wss://ws.test.com/cable
In this one it has not worked (there is no reconnection) and the error is slightly different.
08:20:38.468 [ERR] [WEBSOCKET Test] Error while listening to websocket stream, error: 'The remote party closed the WebSocket connection without completing the close handshake.'
System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.
---> System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond..
---> System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
--- End of inner exception stack trace ---
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
at System.Net.Security.SslStream.<FillBufferAsync>g__InternalFillBufferAsync|215_0[TReadAdapter](TReadAdapter adap, ValueTask`1 task, Int32 min, Int32 initial)
at System.Net.Security.SslStream.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)
at System.Net.Http.HttpConnection.ReadBufferedAsyncCore(Memory`1 destination)
at System.Net.Http.HttpConnection.RawConnectionStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
at System.Net.WebSockets.ManagedWebSocket.EnsureBufferContainsAsync(Int32 minimumRequiredBytes, CancellationToken cancellationToken, Boolean throwOnPrematureClosure)
at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TWebSocketReceiveResultGetter,TWebSocketReceiveResult](Memory`1 payloadBuffer, CancellationToken cancellationToken, TWebSocketReceiveResultGetter resultGetter)
at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TWebSocketReceiveResultGetter,TWebSocketReceiveResult](Memory`1 payloadBuffer, CancellationToken cancellationToken, TWebSocketReceiveResultGetter resultGetter)
at Websocket.Client.WebsocketClient.Listen(WebSocket client, CancellationToken token)
08:29:04.272 [INF] Subscribe = {"command":"subscribe","identifier":"{\"channel\":\"GraphqlChannel\",\"channelId\":\"TEST\"}"}
It's still actual. I have the same behavior with Binance sockets. If I connect to Binance socket with reconnection option, than close the internet connection for 8-9 hours and then turn on - I will get infinite reconnection loop, because after reconnection it has status open, however NativeClient is aborted. The same error in the logs. " - The remote party closed the WebSocket connection without completing the close handshake."