The best way to re-connect after getting web socket error

Jun 6, 2012 at 7:36 PM

I'm trying to re-connect after the network connection goes down.  Once the network connection goes down, I received web socket error (Note: the WebSocket_Error method is called few times) and I called websocket.Close()  in the WebSocket_Error method and wait for the WebSocket_Closed method is called.  Then, I will create a new web socket instance.

I'm wondering what is the best way to re-connect when the connection goes down.  Can I just call webSocket.Close() and not wait for the closed event before calling webSocket.Open()?  Do I need to create a new web socket object to connect or can I just use the same instance and just call webSocket.Open()?  Because when I tried to use the same instance to connect by calling webSocket.Open(), that did not work.  I would expect that to work.

Thanks for  your help.

Jun 7, 2012 at 1:23 AM

In current version, you need top create a new instance to reconnect.

Jul 17, 2012 at 3:35 AM

The latest websocket4net in source code has supported reconnect directly, you needn't create a new instance now.

Aug 7, 2012 at 6:58 PM

I got the latest code and tried to change my reconnect logic (not to create a new instance).  I am having trouble to get it work.  Here is my test case. I put my laptop to hibernate mode.  This will cause the network connection go down.  During the connection is down, I receive a socket error message exception saying

"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."

And, I think the exception is fine.  Once the laptop goes back to normal with network connection.  I call websocket.Open().   And, it doesn't work.  The web socket doesn't try to reconnect.  Is there other methods that we need to call to reconnect?



Aug 7, 2012 at 7:21 PM

Is that truth that we should never call close method if we want to reconnect?

Aug 9, 2012 at 4:41 AM

Could you run the Reconnect test cases (in WebSocket4Net Test project) in NUnit? I think it works.


Which version are you using now? Check the assembly file's property tab.



Aug 13, 2012 at 7:26 PM

I was using a wrong version of the library.   Now I'm using the .8 version and sometime the code stucks in the closing state.   How can we get out of the closing state? 

Aug 14, 2012 at 5:54 PM

The websocket will stuck in the closing state if there isn't a network connection.  After like 5 mins, I plug in the network cable, the socket still stuck in closing state, never change to closed state.

Aug 17, 2012 at 1:33 AM

Could you pull the latest code from source code repository? There was a fix recently about this issue.