ArduinoWebsockets icon indicating copy to clipboard operation
ArduinoWebsockets copied to clipboard

Reconnection problems

Open gdhgdhgdh opened this issue 5 years ago • 11 comments

ESP8266 with Arduino 1.8.13, ArduinoWebsockets 0.5.0

I was reading #75 because I'm having the same probem. I have put my websocket setup calls into its own function:


void onEventsCallback(WebsocketsEvent event, String data) {
    if(event == WebsocketsEvent::ConnectionOpened) {
        Serial.println("Connnection Opened");
    } else if(event == WebsocketsEvent::ConnectionClosed) {
        Serial.println("Connnection Closed");
        setupWebsocket();
    }
}

void setupWebsocket() {
    client = {}; // as advised in issue #75 
    client.onMessage(onMessageCallback);
    client.onEvent(onEventsCallback);
    client.setInsecure(); // yes, lame
    client.connect(websockets_connection_string);
}

void setup() {
// [..  Serial + wifi setup not shown..]

    setupWebsocket();
}

This works fine until the remote websocket is closed - my code does not successfully reconnect :( I DO see the Connection Closed and then shortly after Connection Opened on the Serial output, but I don't get any new incoming WS messages after that.

What am I doing wrong?

gdhgdhgdh avatar Dec 31 '20 18:12 gdhgdhgdh

I guess the problem might be a bug in the copy c'tor of the client class. I wrote this code so long ago, looking at it today it looks like a disaster... Anyways, I recommend avoiding the copy c'tor completely and really allocating a new object. So have the object on the heap and do

delete clientPtr;
clientPtr = new WebsocketsClient(...)

It's a terrible answer, but I think it might be the issue. If anyone wants to jump in and fix this in the library, it is a welcomed contribution. Possible fixes are either refactoring the copy c'tors to include all members, or to just =delete them completely (breaking some user code, but probably fixing so many issues...)

gilmaimon avatar Jan 04 '21 19:01 gilmaimon

Ah thanks for the suggestions.

My C programming days only allow me the faintest grasp of structs, object instantiation and pointer - I threw a bunch of * and & and when it compiled, I only ended up in an ESP8266 loop. I'm happy to simply accept that my little project won't gracefully reconnect :)

gdhgdhgdh avatar Jan 04 '21 22:01 gdhgdhgdh

I guess the problem might be a bug in the copy c'tor of the client class. I wrote this code so long ago, looking at it today it looks like a disaster... Anyways, I recommend avoiding the copy c'tor completely and really allocating a new object. So have the object on the heap and do

It's a terrible answer, but I think it might be the issue. If anyone wants to jump in and fix this in the library, it is a welcomed contribution. Possible fixes are either refactoring the copy c'tors to include all members, or to just =delete them completely (breaking some user code, but probably fixing so many issues...)

Hello! Firstly, I just wanna say thanks for this amazing library.

I have been facing the same issue mentioned here. Instead of keeping the same connection alive with a ping, I want to open a new connection and use that. The Connection opens but doesn't receive any messages.

How exactly is it related to Copy Constructor though? I've commented out one of the copy c'tors but there's no change. And it looks like most of the members, except for functions like onMessage are being copied, and since the setup function is calling onMessage again, I don't think that should be a problem. I would like to work on this, but I think I'll be needing some guidance. What other changes should I be doing to get it working?

Thanks again.

abhishekthalatoty avatar Jul 13 '21 08:07 abhishekthalatoty

Has anyone been able to reconnect after lost connection? Would appreciate a solution.

shardul10 avatar Oct 02 '23 16:10 shardul10

Following @gilmaimon 's suggestion, I simply added this code to instantiate a new Client before the reconnection, so I was able to reconnect and resume the sending of messages to the server. Let's assume we have declared client as a global variable.

WebsocketsClient* clientPtr = &client;
delete clientPtr;
client =  *(new WebsocketsClient());

lukesmolo avatar Oct 18 '23 08:10 lukesmolo

Thank you @lukesmolo. I will try this.

shardul10 avatar Oct 19 '23 11:10 shardul10

@lukesmolo Could you give more explanation on your solution? I am struggeling to implement your snippet to work with reconnections. How did you declare client and is that snippet just for the loop if the conenction fails? I am trying to implement this into the Esp8266-Client.ino. When i try this the ESP seems to completely reboot, that must be wrong?

git-eri avatar Nov 15 '23 23:11 git-eri

@lukesmolo It is not working for my ESP32 project, reconnection issue still apear.

AiziChen avatar Jan 04 '24 03:01 AiziChen

@lukesmolo Please show us the full code....

AviStudio avatar Jan 16 '24 00:01 AviStudio

I need solution for this problem too!

gmm932 avatar Mar 08 '24 12:03 gmm932