ArduinoWebsockets icon indicating copy to clipboard operation
ArduinoWebsockets copied to clipboard

Silly issue: Can't seem to get a websocket server to retain a client.

Open BlueCyro opened this issue 3 years ago • 1 comments

I'm just getting back into arduino/esp8266 programming, however I can't seem to make a websocket server retain a connection after sending one message. I am polling both the client and server:

#include <ArduinoWebsockets.h>


using namespace websockets;

WebsocketsServer heart;

void setup() {
  Serial.begin(115200);
  Serial.println("Fart");

  WiFi.begin("dd-wrt","");

  Serial.println("Connecting");

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("-");
    delay(500);
  }
  Serial.println("Done. Your IP is:");
  Serial.println(WiFi.localIP());

  heart.listen(80);
  
  Serial.println("Setting up server... NOW!");
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
  auto receiver = heart.accept();
  heart.poll();
  receiver.poll();
  
  if ( receiver.available() ) {
    WebsocketsMessage msg = receiver.readBlocking();
    String DATA = msg.data();
    String Command = "/die";
    
    Serial.println(DATA);
  
    if (DATA == Command) {
      Serial.println("Command received");
      receiver.close();
      Serial.println("Dying");
    }
    
  }
}

BlueCyro avatar Sep 23 '20 08:09 BlueCyro

Hi,

You are connecting the client which is running on the esp, to a server running on the same esp?

I think you might be confusing some stuff with your usage.. esp32/8266 are (pretty-much) single threaded so there is really no reason to use a client and server on the same device that talk with each other...

Gil

gilmaimon avatar Sep 25 '20 20:09 gilmaimon