Check websocket connection

This commit is contained in:
Christoph Hagen 2023-11-03 14:33:22 +01:00
parent 5fc450ee63
commit 684df16eb1
5 changed files with 10 additions and 12 deletions

View File

@ -52,7 +52,7 @@ constexpr uint32_t wifiReconnectInterval = 10000;
/* Local server */ /* Local server */
// The port for the local server to directly receive messages over WiFi // The port for the local server to directly receive messages over WiFi
constexpr uint16_t localWebServerPort = 80; constexpr uint16_t localPort = 80;
/* Server */ /* Server */

View File

@ -55,6 +55,8 @@ public:
void connect(); void connect();
void disconnect();
void loop(); void loop();
/** /**
@ -66,16 +68,13 @@ public:
void sendResponse(uint8_t* buffer, uint16_t length); void sendResponse(uint8_t* buffer, uint16_t length);
bool isSocketConnected() { bool isSocketConnected() {
return socketIsConnected; return webSocket.isConnected();
} }
private: private:
ServerConfiguration configuration; ServerConfiguration configuration;
// Indicator that the socket is connected.
bool socketIsConnected = false;
ServerConnectionCallbacks* controller = NULL; ServerConnectionCallbacks* controller = NULL;
// WebSocket to connect to the control server // WebSocket to connect to the control server

View File

@ -4,9 +4,6 @@
#include <WiFi.h> #include <WiFi.h>
// The url parameter to send the message to the local server
constexpr char messageUrlParameter[] = "m";
SesameController::SesameController(uint16_t localWebServerPort, uint8_t remoteDeviceCount) : SesameController::SesameController(uint16_t localWebServerPort, uint8_t remoteDeviceCount) :
storage(remoteDeviceCount), localWebServer(localWebServerPort) { storage(remoteDeviceCount), localWebServer(localWebServerPort) {

View File

@ -13,7 +13,7 @@
#include "controller.h" #include "controller.h"
#include "config.h" #include "config.h"
SesameController controller(localWebServerPort, remoteDeviceCount); SesameController controller(localPort, remoteDeviceCount);
void setup() { void setup() {
Serial.begin(serialBaudRate); Serial.begin(serialBaudRate);

View File

@ -12,7 +12,7 @@ void ServerConnection::configure(ServerConfiguration configuration, ServerConnec
} }
void ServerConnection::connect() { void ServerConnection::connect() {
if (socketIsConnected) { if (webSocket.isConnected()) {
return; return;
} }
if (controller == NULL) { if (controller == NULL) {
@ -29,6 +29,10 @@ void ServerConnection::connect() {
webSocket.setReconnectInterval(configuration.reconnectTime); webSocket.setReconnectInterval(configuration.reconnectTime);
} }
void ServerConnection::disconnect() {
webSocket.disconnect();
}
void ServerConnection::loop() { void ServerConnection::loop() {
webSocket.loop(); webSocket.loop();
} }
@ -36,11 +40,9 @@ void ServerConnection::loop() {
void ServerConnection::webSocketEventHandler(WStype_t type, uint8_t * payload, size_t length) { void ServerConnection::webSocketEventHandler(WStype_t type, uint8_t * payload, size_t length) {
switch(type) { switch(type) {
case WStype_DISCONNECTED: case WStype_DISCONNECTED:
socketIsConnected = false;
Serial.println("[INFO] Socket disconnected."); Serial.println("[INFO] Socket disconnected.");
break; break;
case WStype_CONNECTED: case WStype_CONNECTED:
socketIsConnected = true;
webSocket.sendTXT(configuration.key); webSocket.sendTXT(configuration.key);
Serial.printf("[INFO] Socket connected to url: %s\n", payload); Serial.printf("[INFO] Socket connected to url: %s\n", payload);
webSocket.enableHeartbeat(pingInterval, pongTimeout, disconnectTimeoutCount); webSocket.enableHeartbeat(pingInterval, pongTimeout, disconnectTimeoutCount);