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

poll() disconnects client #139

Closed
the-powl opened this issue Nov 21, 2022 · 7 comments
Closed

poll() disconnects client #139

the-powl opened this issue Nov 21, 2022 · 7 comments
Assignees

Comments

@the-powl
Copy link

the-powl commented Nov 21, 2022

I'm trying to call poll() in a loop for checking on incoming connections and if true create a WebsocketsClient named client. As soon as a client is connected though, it gets disconnected again when the loop rolls over and reaches if(server.poll()) again. I found out via placing Serial.println() commands in between every step. If I never call poll() again, the client never disonnects.

I'm using it on an ESP8266. Library version is 0.5.3. Websocket connection is established via a simple html/js document, which does not initiate the disconnect on its own.

Here's a minimized code:

void loop() {
  static WebsocketsClient client;
 
  if(server.poll()) {
    client = server.accept();
    Serial.println("new client!");
  }

  if(client.available()) {
    WebsocketsMessage msg = client.readBlocking();

    // log
    Serial.print("Got Message: ");
    Serial.println(msg.data());

    // return echo
    client.send("Echo: " + msg.data());
    Serial.println("Echo: " + msg.data());
  }

  delay(1000);
}

Am I doing something wrong?

@zastrixarundell
Copy link
Contributor

Haven't use this library in a long time but:

You are polling again which is fine but if it's true you again accept the connection, that would be an issue, no?

What happens when you only poll it, does it disconnect again?

@the-powl
Copy link
Author

After accepting the first connection, the result of poll() is false, as expected, so therefore the if block doesn't get executed. Nevertheless the client gets disconnected.

@zastrixarundell
Copy link
Contributor

zastrixarundell commented Nov 26, 2022

Okay, you're connecting first, then you are reading the message in a blocking manner and then you additionally wait one second. I don't see you sending a heartbeat to the server so maybe the server doesn't recognize you as connected anymore? I know that I had an issue like that and I was sending a heartbeat connection every second or so.

I used lambdas to receive messages from the client without actually stopping the main loop. Here's the directory with both my declaration and implementation if that helps/

Edit: Here's my loop as well.

@the-powl
Copy link
Author

the-powl commented Dec 3, 2022

Unfortunately I'm not sure how to send a heartbeat and I don't just want to send random garbage. Shouldn't the TCP connection be maintained by the library itself or by an underlying level? Interestingly I can also wait more than 1 second... like 5 or even 10. The client gets only disconnected as soon when I call poll() again.

@zastrixarundell
Copy link
Contributor

zastrixarundell commented Dec 4, 2022 via email

@the-powl
Copy link
Author

the-powl commented Dec 4, 2022

omg I'm stupid. I oversaw a websocket.close() in my partly copy and pasted javascript code which was responsible for closing the connection. I got suspicious when I tried to use another arduino library and it still didn't work 🤣. Thanks for help!

@the-powl the-powl closed this as completed Dec 4, 2022
@zastrixarundell
Copy link
Contributor

omg I'm stupid

Yep, that about sums 1/2 of the problems when being a dev. Cheers!

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