I make a device on esp8266, which connects to php Workerman websocket server via websocket using WebSocketsClient.h library. Everything works, connection is established. But when I turn off the device, the server doesn't receive the onClose signal. And I need to constantly monitor whether there is a connection with the device. However, when I connect via websocket using a browser and close the tab, everything works fine and the server receives a signal that the connection is closed. How can I make the server see this when disconnecting esp8266?
Websocket server.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
use Workerman\Lib\Timer;
$context = array(
'ssl' => array(
'local_cert' => '/etc/letsencrypt/live/***.com/fullchain.pem',
'local_pk' => '/etc/letsencrypt/live/***.com/privkey.pem',
'verify_peer' => false,
'allow_self_signed' => true,
'verify_peer_name' => false,
)
);
$ws_worker = new Worker("websocket://***.com:8888", $context);
$ws_worker->transport = 'ssl';
$ws_worker->onConnect = function($connection) use (&$devices)
{
echo "Connection id: ".$connection->id.PHP_EOL;
};
$ws_worker->onClose = function($connection) use (&$user_connections)
{
echo '---> DISCONNECT';
};
Worker::runAll();
esp8266 code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WebSocketsClient.h>
const char* ssid = "ESP32";
const char* pwd = "01234567";
WebSocketsClient webSocket;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pwd);
while(WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(100);
}
webSocket.beginSSL("***.com", 8888,url.c_str());
webSocket.onEvent(webSocketEvent);
webSocket.setReconnectInterval(5000);
}
void loop() {
webSocket.loop();
}
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("Disconnected!\n");
break;
case WStype_CONNECTED:
Serial.println("SENT: Connected");
break;
case WStype_TEXT:
Serial.printf("RESPONSE: %s\n", payload);
json_error = deserializeJson(doc, payload);
break;
case WStype_BIN:
Serial.printf("[WSc] get binary length: %u\n", length);
hexdump(payload, length);
break;
case WStype_PING:
Serial.printf("[WSc] get ping\n");
break;
case WStype_PONG:
Serial.printf("[WSc] get pong\n");
break;
}
}
I tried turning on the esp8266:
webSocket.enableHeartbeat(10000, 5000, 2)
but that didn't work. When I turn off the esp8266 device, the server does not see that it is turned off.