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

How to handle disconnect or an error #13

Closed
johneakin opened this issue Jun 1, 2019 · 4 comments
Closed

How to handle disconnect or an error #13

johneakin opened this issue Jun 1, 2019 · 4 comments
Assignees
Labels
resolved the issue was resolved usage queshtion Queshtions about using the library, its interface and so on

Comments

@johneakin
Copy link

What is the best way to reconnect after a disconnect or some error?

I tried the following but doesn't appear to be working.
void onEventsCallback(WebsocketsEvent event, String data) {
if(event == WebsocketsEvent::ConnectionClosed) {
client.connect("wss://ws-feed.pro.coinbase.com");
client.send("{"type": "subscribe","product_ids": ["BTC-USD"],"channels": ["ticker"]}");
}

Instead is there any way to check if the connection is active in the loop() and reconnect from there?

@gilmaimon gilmaimon added the usage queshtion Queshtions about using the library, its interface and so on label Jun 1, 2019
@gilmaimon
Copy link
Owner

Hi, the best way to check if a client is connected is by using:

if (client.available()) {
   // Connected...
}

The best way to reconnect is to reset the object (by assinging a new instance) and setting up the callbacks again. So for example:

if (client.available()) {
   // Connected, do something...
} else {
    client = WebsocketsClient();
    client.onMessage(...);
    client.onEvent(...);
    client.connect(...);
}

@gilmaimon gilmaimon self-assigned this Jun 1, 2019
@gilmaimon gilmaimon added the resolved the issue was resolved label Jun 7, 2019
@gilmaimon
Copy link
Owner

I consider this issue resolved. If you have any other queshtions please share, otherwise I will close this issue due to inactivity.

@gilmaimon
Copy link
Owner

Resolved.

@alldone
Copy link

alldone commented Feb 9, 2020

Hi, the best way to check if a client is connected is by using:

if (client.available()) {
   // Connected...
}

The best way to reconnect is to reset the object (by assinging a new instance) and setting up the callbacks again. So for example:

if (client.available()) {
   // Connected, do something...
} else {
    client = WebsocketsClient();
    client.onMessage(...);
    client.onEvent(...);
    client.connect(...);
}

this is what i was looking for ....... however it is an excellent library, a great job.
thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
resolved the issue was resolved usage queshtion Queshtions about using the library, its interface and so on
Projects
None yet
Development

No branches or pull requests

3 participants