websocket-client
websocket-client copied to clipboard
Throwing Exception to reconnect even the Websocket got closed
I closed the websocket after performing some task but in logs file this exception is coming again and again even the websocket connection is closed. Any why why it is throwing this exception.
Exception while connecting. Waiting 60 sec before next reconnection try. Error: 'Cannot access a disposed object. Object name: 'System.Net.WebSockets.ClientWebSocket'.'] [Exception :System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.WebSockets.ClientWebSocket'. at System.Net.WebSockets.ClientWebSocket.ConnectAsync(Uri uri, CancellationToken cancellationToken) at Websocket.Client.WebsocketClient.<>c__DisplayClass65_0.<<GetClientFactory>b__0>d.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 Websocket.Client.WebsocketClient.<StartClient>d__68.MoveNext()
this is code i am using to connect ClientWebSocket clientWebSocket = new ClientWebSocket();
// Create the factory with Native Object of Client Websocket
var factory = new Func<ClientWebSocket>(() => clientWebSocket);
// Init client object with URI and Factory object
_client = new WebsocketClient(new Uri("wss//:xxxxxxxxxxxxxxxxxxxxxxxx), factory);
// Reconnect time set to 30 seconds -> Will reconect if it fails due to network issue
_client.ReconnectTimeout = TimeSpan.FromSeconds(30);
// Subscribe over Reconnection and it will print in the console about reconection
_client.ReconnectionHappened.Subscribe(ReconnectionOccured);
// Message received will be posted to Receive Method
_client.MessageReceived.Subscribe(Receive);
// Let's connect to WSS
_client.Start();
Hello @ShoaibShahid,
you can read about it in code documentation on the IWebsocketClient interface:
So ReconnectTimeout
just affects inactivity, if no message comes from server it performs reconnection.
There is a second configuration ErrorReconnectTimeout
which is this 60 sec by default, it tries to reconnect when connection fails on network level.
You can always disable everything by setting null values,
_client.ErrorReconnectTimeout = null
or
_client.ReconnectTimeout= null