processing_websockets
processing_websockets copied to clipboard
Reconnecting to server after failure
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.
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.
Actually, I have never built a library like this.
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.
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).
Thanks @hamoid. That's very useful advice. I'll try to give it a crack in the weekend.
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?
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.
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!