processing_websockets icon indicating copy to clipboard operation
processing_websockets copied to clipboard

Reconnecting to server after failure

Open botherder opened this issue 5 years ago • 8 comments

In order to make a failproof installation, I need to make sure that a WebsocketClient connection can be re-established after a temporary failure from the server. While I can just periodically enforce e reconnection regardless, it would be useful to be able to monitor for events or check the connection status of the client. Is there any way you suggest to do this? Thanks for your work.

botherder avatar Feb 09 '20 22:02 botherder

Hi, do you know how to compile the library yourself?

In this line https://github.com/alexandrainst/processing_websockets/blob/master/src/websockets/WebsocketServerEvents.java#L52 you could forward the statusCode and the reason to ctrl.remove(), then check those values and decide what to do about them.

hamoid avatar Feb 10 '20 08:02 hamoid

Actually, I have never built a library like this.

botherder avatar Feb 11 '20 13:02 botherder

I looked into it. I pointed you at the wrong file. The right files are WebsocketClient.java and WebsocketClientEvents.java. In the second file, find @OnWebSocketError. When that happens it should call a method on the first file. Both files have two Method. I would add a third one for errors to then call a user function on errors. That's one option. Another option is to just have a public property indicating if the connection is active or not.

To compile the library you can open the project (the .iml file) using IntelliJ Idea, then run Build > Generate Artifacts which will put the new jar file into this folder: out/artifacts/processing_websockets_jar/. You can move it from there replacing the old version inside the processing libraries folder.

Unfortunately no time to work on this now.

hamoid avatar Feb 12 '20 11:02 hamoid

A fancier version would deal with reconnecting automatically inside the library, and either cue or discard messages when trying to send them while the connection is down (currently it will throw an exception and terminate).

hamoid avatar Feb 12 '20 11:02 hamoid

Thanks @hamoid. That's very useful advice. I'll try to give it a crack in the weekend.

botherder avatar Feb 12 '20 16:02 botherder

I've tried to implement the first suggestion here: https://github.com/botherder/processing_websockets

I had to change a few things, and now a webSocketOnError method is called in the sketch by onError. Unfortunately, this doesn't seem to help, as the sketch hangs I guess as described in #6.

Any ideas?

botherder avatar Mar 01 '20 14:03 botherder

I have sort of resolved this by doing a sendMessage() every few seconds. If it fails because of NullPointer then I set a boolean flag instructing to recreate the WebsocketClient. I have commented out the latch.await() in my fork, as it is what cause the sketch to hang if the connection fails.

This is really hacky though. I think proper exception handling is lacking at various levels.

botherder avatar Mar 01 '20 17:03 botherder

Hey @botherder , thanks a lot for your fork! As you mentioned, I'm calling sendMessage() periodically to see if it fails. Just creating a new WebsocketClient when this happens is not a working solution, as the resources from the client are not freed and the used RAM keeps increasing until crash happens. Finally I added a reconnect method which calls stop(), start() and connect() on the client and is working nicely. Thanks!

kosowski avatar Sep 01 '22 17:09 kosowski