Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnecting to server after failure #20

Open
botherder opened this issue Feb 9, 2020 · 8 comments
Open

Reconnecting to server after failure #20

botherder opened this issue Feb 9, 2020 · 8 comments

Comments

@botherder
Copy link

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.

@hamoid
Copy link
Contributor

hamoid commented Feb 10, 2020

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.

@botherder
Copy link
Author

Actually, I have never built a library like this.

@hamoid
Copy link
Contributor

hamoid commented Feb 12, 2020

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
Copy link
Contributor

hamoid commented Feb 12, 2020

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

@botherder
Copy link
Author

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

@botherder
Copy link
Author

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
Copy link
Author

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.

@kosowski
Copy link

kosowski commented Sep 1, 2022

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants