Scarlet icon indicating copy to clipboard operation
Scarlet copied to clipboard

0.2.x: SocketIoClient can't reconnect when receiving "disconnect" from Server

Open mikef-dk opened this issue 4 years ago • 3 comments

While investigating an issue with our Socket Connection I noticed that Scarlets' SocketIoClient has a problem when we are trying to reconnect after the Server triggered a disconnect.

To go further into details:

When we receive a Socket.EVENT_DISCONNECT in the SocketIoMainChannel#open the triggered listener / event queue eventually triggers the SocketIoMainChannel#close method which disconnects and nulls the Socket. However when we are trying to reconnect by emitting the LifecycleState.Started event via the LifecycleRegistry we end up in the SocketIoMessageChannel#open method which checks whether the parent socket (the one from SocketIoMainChannel) is present or not. Obviously it's not since it was nulled. As a result a retry is triggered which never succeeds since the parent socket is not "re-established".

Problem in Scarlet version 0.2.4

mikef-dk avatar Jul 01 '20 06:07 mikef-dk

@mikef-dk were you able to solve your problem?

miguelhrocha avatar Jul 31 '20 14:07 miguelhrocha

I was however I consider my solution more of a workaround than a proper fix (though it works flawlessly). I created my own SocketIoClient which basically does the same as the default one but solves this issue as well as this one regarding JSONArray. Check out the following gist

https://gist.github.com/mikef-dk/dcb5466a1dd8070577f808572159c3e0

I've added a #reconnect method to the SocketIoMainChannel which explicitly tries to open the socket yet again if a Socket.EVENT_DISCONNECT was received. This way - if successful - the SocketIoMessageChannel#open method can proceed without being stuck in an endless loop.

Changes for Issue #89 https://gist.github.com/mikef-dk/dcb5466a1dd8070577f808572159c3e0#file-customsocketioclient-kt-L151-L156

Changes for Issue #145 https://gist.github.com/mikef-dk/dcb5466a1dd8070577f808572159c3e0#file-customsocketioclient-kt-L124-L128

mikef-dk avatar Jul 31 '20 14:07 mikef-dk

Thanks @mikef-dk you truly inspired me to find the solution. I was using another protocol, and thanks to your solution I just modified the onClosing method in the WebSocketListener to trigger a failed operation :) Dirty but works

miguelhrocha avatar Jul 31 '20 17:07 miguelhrocha