From 684df16eb18ddcc8dbf49f3e123e668aa755f8e1 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Fri, 3 Nov 2023 14:33:22 +0100 Subject: [PATCH] Check websocket connection --- include/example_config.h | 2 +- include/server.h | 7 +++---- src/controller.cpp | 3 --- src/main.cpp | 2 +- src/server.cpp | 8 +++++--- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/include/example_config.h b/include/example_config.h index 44290ee..5f767d9 100644 --- a/include/example_config.h +++ b/include/example_config.h @@ -52,7 +52,7 @@ constexpr uint32_t wifiReconnectInterval = 10000; /* Local server */ // The port for the local server to directly receive messages over WiFi -constexpr uint16_t localWebServerPort = 80; +constexpr uint16_t localPort = 80; /* Server */ diff --git a/include/server.h b/include/server.h index 72b460f..3767262 100644 --- a/include/server.h +++ b/include/server.h @@ -55,6 +55,8 @@ public: void connect(); + void disconnect(); + void loop(); /** @@ -66,16 +68,13 @@ public: void sendResponse(uint8_t* buffer, uint16_t length); bool isSocketConnected() { - return socketIsConnected; + return webSocket.isConnected(); } private: ServerConfiguration configuration; - // Indicator that the socket is connected. - bool socketIsConnected = false; - ServerConnectionCallbacks* controller = NULL; // WebSocket to connect to the control server diff --git a/src/controller.cpp b/src/controller.cpp index 5e694c0..f879589 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -4,9 +4,6 @@ #include -// The url parameter to send the message to the local server -constexpr char messageUrlParameter[] = "m"; - SesameController::SesameController(uint16_t localWebServerPort, uint8_t remoteDeviceCount) : storage(remoteDeviceCount), localWebServer(localWebServerPort) { diff --git a/src/main.cpp b/src/main.cpp index 6fe050b..ffde7a5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,7 +13,7 @@ #include "controller.h" #include "config.h" -SesameController controller(localWebServerPort, remoteDeviceCount); +SesameController controller(localPort, remoteDeviceCount); void setup() { Serial.begin(serialBaudRate); diff --git a/src/server.cpp b/src/server.cpp index b17db2a..1009932 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -12,7 +12,7 @@ void ServerConnection::configure(ServerConfiguration configuration, ServerConnec } void ServerConnection::connect() { - if (socketIsConnected) { + if (webSocket.isConnected()) { return; } if (controller == NULL) { @@ -29,6 +29,10 @@ void ServerConnection::connect() { webSocket.setReconnectInterval(configuration.reconnectTime); } +void ServerConnection::disconnect() { + webSocket.disconnect(); +} + void ServerConnection::loop() { webSocket.loop(); } @@ -36,11 +40,9 @@ void ServerConnection::loop() { void ServerConnection::webSocketEventHandler(WStype_t type, uint8_t * payload, size_t length) { switch(type) { case WStype_DISCONNECTED: - socketIsConnected = false; Serial.println("[INFO] Socket disconnected."); break; case WStype_CONNECTED: - socketIsConnected = true; webSocket.sendTXT(configuration.key); Serial.printf("[INFO] Socket connected to url: %s\n", payload); webSocket.enableHeartbeat(pingInterval, pongTimeout, disconnectTimeoutCount);