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

error handling on disconnect #38

Closed
antimodular opened this issue Sep 20, 2014 · 6 comments
Closed

error handling on disconnect #38

antimodular opened this issue Sep 20, 2014 · 6 comments

Comments

@antimodular
Copy link

i am trying to figure out how to catch my client getting disconnected from the server. for example when i unplug the ethernet cable onClose is not called. an onError would be great :)

i should mention that my client only listens to incoming messages.
i tried if(client.getConnection() == false) but this only is set to false if the network connection is not present before and right at start of app.

thanks, stephan

@robotconscience
Copy link
Owner

Hello! Looking into this right now.

Basically, it has to do with how the underlying library we use works. Basically, unless libwebsockets gets an official "close" from a server (a "clean" close event like someone manually closing the connection), it doesn't fire an event. It does, however, give you options for setting up the client with a timeout.

I need to do a bit of an overhaul to the ClientOptions to get this integrated, but if you'd like to test it now, add the following lines to around line 99 in Client.cpp:

        info.ka_time = 1;
        info.ka_probes = 1;
        info.ka_interval = 1;

The bad news is that the timeouts are in seconds... So for me it took about 5 seconds (on OS X) to get a "close" event from turning my network off. You may be able to play with ka_probes (how many times it tests the network) and ka_interval (time between the checks) to get a more real time response.

@antimodular
Copy link
Author

nice. i will try that tomorrow and report back.
thanks a bunch.

@antimodular
Copy link
Author

i tried it but nothing happened. no onClose or anything else got called.

i placed it in bool Client::connect ( ClientOptions options )

right after:
info.port = CONTEXT_PORT_NO_LISTEN;
info.protocols = &lws_protocols[0];
info.extensions = libwebsocket_get_internal_extensions();
info.gid = -1;
info.uid = -1;

@robotconscience
Copy link
Owner

Yep, that's the right spot. Hm!

What platform are you on? And what are you doing to spark the "onClose"? I was able to get it to fire by turning off my network, but it did take quite some time to do so. Not sure the best way to make it any faster.

If you change line 50 in Util.h from
ofLog( OF_LOG_VERBOSE, getCallbackReason(reason) );
to
ofLog( OF_LOG_ERROR, getCallbackReason(reason) );

You'll get a printout of all of the libwebsockets events. Try setting that then doing your test again; after 30-60 seconds you should see LWS_CALLBACK_CLOSED, then the onClose event get fired in your main app.

@antimodular
Copy link
Author

just to note that with your original suggestion i do get reconnected successfully once i replug my ethernet cable. so, that helps a lot already.

ok will try you latest advice too.
thanks.

@robotconscience
Copy link
Owner

About to push ability to set these params via ClientOptions. However, libwebsockets uses whatever your system defaults are on OpenBSD platforms (e.g. OS X). Setting "ka_time" to anything but 0 will at least check if alive periodically, but at whatever your global settings are.

On my machine (OS X 10.9.4), that timeout is about 30 seconds!

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

No branches or pull requests

1 participant