gilmaimon / ArduinoWebsockets

A library for writing modern websockets applications with Arduino (ESP8266 and ESP32)
GNU General Public License v3.0
480 stars 97 forks source link

Secured-ESP8266-Client can't seem to receive any messages while connection is open #143

Closed Subject-2407 closed 1 year ago

Subject-2407 commented 1 year ago

Hi, recently I'm making an IoT project which needs a secured websocket connection (SSL). Then I found and tried this library which almost made my project works perfectly than any library I've tried. The problem is, my ESP8266 can only receive a message only when it's successfully connected to my websocket server. After that, it won't receive any messages whenever an event occurs.

Just a slight addition (wifi and server) to the Secured Client example, but here's my whole code.

#include <ArduinoWebsockets.h>
#include <ESP8266WiFi.h>

const char* ssid = "hotspot1024"; //Enter SSID
const char* password = "12345678"; //Enter Password

const char* websockets_connection_string = "wss://ecato.my.id:6002/app/ABCDEFG?protocol=7&client=js&version=4.3.1&flash=false"; //Enter server address

// This latest SHA1 fingerprint was updated 15.04.2021
const char echo_org_ssl_fingerprint[] PROGMEM   = "B4 A0 D6 76 1B 68 01 54 43 A7 33 45 AB D5 73 CE 06 09 40 83";

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!");
    }
}

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

    for(int i = 0; i < 10 && WiFi.status() != WL_CONNECTED; i++) {
        Serial.print(".");
        delay(1000);
    }

    // run callback when messages are received
    client.onMessage(onMessageCallback);

    // run callback when events are occuring
    client.onEvent(onEventsCallback);

    // Before connecting, set the ssl fingerprint of the server
    client.setFingerprint(echo_org_ssl_fingerprint);

    // Connect to server
    client.connect(websockets_connection_string);

    // Send a message
    client.send("Hello Server");

    // Send a ping
    client.ping();
}

void loop() {
    client.poll();
}

And here's my serial log. ...... Connnection Opened Got Message: {"event":"pusher:connection_established","data":"{\"socket_id\":\"539968520.540064775\",\"activity_timeout\":30}"} Got a Pong!

Here's my websocket server dashboard image The red highlighted square is when the ESP8266 connection successfully established. The blue hightlighted square is when I sent an event.

Did I do something wrong with my code or it's just the server-side that's causing the problem?

Subject-2407 commented 1 year ago

Never mind, I just had to subscribe to a channel of my websocket server through client.send(). Closing this issue.