#include #include // === A. === obviously, used for ESP8266. const char* ssid = "[hidden]"; //Enter SSID const char* password = "[hidden]"; //Enter Password const char* websockets_server = "wss://[hidden]/ws/"; //server adress and port int i; using namespace websockets; void onMessageCallback(WebsocketsMessage message) { Serial.print("Got Message: "); Serial.print(message.data()); // === A. === Prints the sequence (loop) number along with the message Serial.print(" | Sequence no: "); Serial.println(i); } 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!"); } // === A. === Wrote this line just to see if there's another type of event that is triggered, that is not being caught 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); } // === A. === Just for debugging purposes, as there's no real verification Serial.println("Connected to WiFi... probably."); // Setup Callbacks client.onMessage(onMessageCallback); client.onEvent(onEventsCallback); // Connect to server client.connect(websockets_server); // Send a message // === A. === This is the way the server replies... in JSON, so I'm using this line to test. client.send("{\"type\":\"user-msg\",\"name\":\"Jess D_Arduino\",\"message\":\"I`m just surfing a bit the socket...\"}"); } // === A. === I'm verifying in the loop at every 1kk loop if I'm still connected, this way I can see if it sees the nginx disconnect or not. void loop() { if (client.available()) { client.poll(); if(i++ % 100000 == 0) Serial.println("Client still connected."); } else { Serial.println("Client Disconnected!"); delay(5000); } }