Links2004 / arduinoWebSockets

arduinoWebSockets
GNU Lesser General Public License v2.1
1.85k stars 549 forks source link

The websocket connection frequently connects and disconnect and sends no data. #790

Open hamidtb opened 1 year ago

hamidtb commented 1 year ago

hi I recently wrote code in my ESP32 using your library, but the server log showed many connects and disconnects, and I received no data from the server. This is my code.

#include <Arduino.h>
#include <WiFiMulti.h>

#include <WebSocketsClient.h>
#include <ESP32Ping.h>

WiFiMulti objWifiMulti;
WebSocketsClient objWebSocketClient;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("\n\nBooting; please wait...");
  delay(2000);

  pinMode(2, OUTPUT);
  objWifiMulti.addAP("***", "***");
  objWifiMulti.addAP("***", "*");

  Serial.print("\n\nBoot complete; connecting to wifi network.");

  while (objWifiMulti.run() != WL_CONNECTED)
  {
    Serial.print(".");
    digitalWrite(2, !digitalRead(2));
    delay(1000);
  }
  Serial.println("\nconnecting to WIFI network successful; assign ip is: " + WiFi.localIP().toString());

  digitalWrite(2, HIGH);

  objWebSocketClient.beginSSL("ehome.iran.liara.run",443,"/","","wss");

  objWebSocketClient.setReconnectInterval(5000);

  //objWebSocketClient.onEvent(webSocketEvent);
  objWebSocketClient.sendTXT("Hello FROM ESP32!");
  //objWebSocketClient.begin("ehome.iran.liara.run", 443, "/", "wss");
}

void loop()
{
  // put your main code here, to run repeatedly:
  /*Ping.ping("8.8.8.8");
  Serial.print("ping IP 8.8.8.8 in ");
  Serial.print(Ping.averageTime());
  Serial.println(" ms.");
  delay(1000);*/
  objWebSocketClient.sendTXT("Hello FROM ESP32!");
  delay (6000);
  objWebSocketClient.loop();

}
Links2004 commented 1 year ago

the delay is your loop is most likely the problem. the headbeat from the server is missed, since you call the WS loop function only every 6sec. try to use this pattern for running code in intervals instead of the delay.

static unsigned long last_run;
void loop() {
  unsigned long now = millis();
  objWebSocketClient.loop();

  if(now - last_run >= 6000) {
    objWebSocketClient.sendTXT("Hello FROM ESP32!");
    last_run = millis();
  }
}

Note: code not tested, but sure works a a hint ;)

hamidtb commented 1 year ago

thanks a lot; Its work perfect

Is that code cause Pressure on the device's CPU?

I also see two functions: 1-websocketclient.enableHeartbeat(); 2-websocketclient.sendping();

How to use them to keep the connection alive?

Links2004 commented 1 year ago

sendping can be used to send one ping manually, this is mostly use full for checking if the TCP connection is still working. enableHeartbeat automates sending pings and waiting for the response (and on timeout reconnecting).

all is handled inside the loop function, and only works it loop is called regularly.

after the connection is established the loop function does only does check if new TCP data is there form there server and the timeout handling. if there is no new data the function exits quickly so no big resource usage there. the arduino TCP interface is poll only so no way around it ;)