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 */
// The port for the local server to directly receive messages over WiFi
constexpr uint16_t localWebServerPort = 80;
constexpr uint16_t localPort = 80;
/* Server */

View File

@ -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

View File

@ -4,9 +4,6 @@
#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) :
storage(remoteDeviceCount), localWebServer(localWebServerPort) {

View File

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

View File

@ -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);