Skip to content

Library does not handle server/network failure (disconnect basically). #14

@adelin-mcbsoft

Description

@adelin-mcbsoft

Hi,

I'm using the code from the Client Example for an SSL server.
Most of it works as expected, however - if I shutdown the WebSocket server I'm connected to or even disconnect the network, no event is being triggered.
Surprisingly, even if i call client.available() it returns true, even though the server is shut down or there's no network connection to it...

I'm using an ESP-WROOM-32 board if and an NGinx WebSocket server. The socket server works without any issue with any browser client (tested EDGE, Firefox, Chrome) and these browsers successfully report a server disconnect or so.

As told, code is the one from the client example:

#include <ArduinoWebsockets.h>
#include <WiFi.h>

const char* ssid = "WiFi Name"; //Enter SSID
const char* password = "WiFi Password"; //Enter Password
const char* websockets_server = "wss://url.com:443/ws/"; //server adress and port

using namespace websockets;

void onMessageCallback(WebsocketsMessage message) {
    Serial.print("Got Message: ");
    Serial.println(message.data());
}

void onEventsCallback(WebsocketsEvent event, String data) {
    if(event == WebsocketsEvent::ConnectionOpened) {
        Serial.println("Connnection Opened");
    } else if(event == WebsocketsEvent::ConnectionClosed) {
        Serial.println("Connnection Closed");
    } else if(event == WebsocketsEvent::GotPing) {
        Serial.println("Got a Ping!");
    } else if(event == WebsocketsEvent::GotPong) {
        Serial.println("Got a Pong!");
    }

    Serial.println("Event triggered...");
}

WebsocketsClient client;
void setup() {
    Serial.begin(115200);
    // Connect to wifi
    WiFi.begin(ssid, password);

    // Wait some time to connect to wifi
    for(int i = 0; i < 10 && WiFi.status() != WL_CONNECTED; i++) {
        Serial.println(".");
        delay(1000);
    }

    Serial.println("Connected to WiFi... probably.");

    // Setup Callbacks
    client.onMessage(onMessageCallback);
    client.onEvent(onEventsCallback);
    
    // Connect to server
    client.connect(websockets_server);

    // Send a message
    client.send("This is a nice little message sent from ESP32.");
}

void loop() {
  if (client.available()) {
    client.poll();
  } else {
    Serial.println("OOPS!! Client Disconnected...");
    delay(5000);
  }
}

If there's any detail I can help you with to debug this issue, just let me know.
All the best,
A.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingresolvedthe issue was resolved

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions