8000 Implemented HeartBeat in WebSocketsServer · DerialE/arduinoWebSockets@674a6e9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 674a6e9

Browse files
committed
Implemented HeartBeat in WebSocketsServer
1 parent c038f10 commit 674a6e9

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/WebSocketsServer.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ WebSocketsServer::WebSocketsServer(uint16_t port, String origin, String protocol
3030
_origin = origin;
3131
_protocol = protocol;
3232
_runnning = false;
33+
_pingInterval = 0;
34+
_pongTimeout = 0;
35+
_disconnectTimeoutCount = 0;
3336

3437
_server = new WEBSOCKETS_NETWORK_SERVER_CLASS(port);
3538

@@ -91,6 +94,10 @@ void WebSocketsServer::begin(void) {
9194
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
9295
client->cHttpLine = "";
9396
#endif
97+
98+
client->pingInterval = _pingInterval;
99+
client->pongTimeout = _pongTimeout;
100+
client->disconnectTimeoutCount = _disconnectTimeoutCount;
94101
}
95102

96103
#ifdef ESP8266
@@ -486,6 +493,12 @@ bool WebSocketsServer::newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient) {
486493
client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsServer::handleHeader, this, client, &(client->cHttpLine)));
487494
#endif
488495

496+
client->pingInterval = _pingInterval;
497+
client->pongTimeout = _pongTimeout;
498+
client->disconnectTimeoutCount = _disconnectTimeoutCount;
499+
client->lastPing = millis();
500+
client->pongReceived = false;
8000
501+
489502
return true;
490503
break;
491504
}
@@ -677,6 +690,9 @@ void WebSocketsServer::handleClientData(void) {
677690
break;
678691
}
679692
}
693+
694+
handleHBPing(client);
695+
handleHBTimeout(client);
680696
}
681697
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
682698
delay(0);
@@ -854,3 +870,50 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
854870
}
855871
}
856872
}
873+
874+
/**
875+
* send heartbeat ping to server in set intervals
876+
*/
877+
void WebSocketsServer::handleHBPing(WSclient_t * client) {
878+
if(client->pingInterval == 0)
879+
return;
880+
uint32_t pi = millis() - client->lastPing;
881+
if(pi > client->pingInterval) {
882+
DEBUG_WEBSOCKETS("[WS-Server][%d] sending HB ping\n", client->num);
883+
if(sendPing(client->num)) {
884+
client->lastPing = millis();
885+
client->pongReceived = false;
886+
}
887+
}
888+
}
889+
890+
/**
891+
* enable ping/pong heartbeat process
892+
* @param pingInterval uint32_t how often ping will be sent
893+
* @param pongTimeout uint32_t millis after which pong should timout if not received
894+
* @param disconnectTimeoutCount uint8_t how many timeouts before disconnect, 0=> do not disconnect
895+
*/
896+
void WebSocketsServer::enableHeartbeat(uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount) {
897+
_pingInterval = pingInterval;
898+
_pongTimeout = pongTimeout;
899+
_disconnectTimeoutCount = disconnectTimeoutCount;
900+
901+
WSclient_t * client;
902+
for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
903+
client = &_clients[i];
904+
WebSockets::enableHeartbeat(client, pingInterval, pongTimeout, disconnectTimeoutCount);
905+
}
906+
}
907+
908+
/**
909+
* disable ping/pong heartbeat process
910+
*/
911+
void WebSocketsServer::disableHeartbeat() {
912+
_pingInterval = 0;
913+
914+
WSclient_t * client;
915+
for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
916+
client = &_clients[i];
917+
client->pingInterval = 0;
918+
}
919+
}

src/WebSocketsServer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ class WebSocketsServer : protected WebSockets {
9191
void setAuthorization(const char * auth);
9292

9393
int connectedClients(bool ping = false);
94+
95+
void enableHeartbeat(uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount);
96+
void disableHeartbeat();
9497

9598
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
9699
IPAddress remoteIP(uint8_t num);
@@ -112,6 +115,10 @@ class WebSocketsServer : protected WebSockets {
112115
WebSocketServerHttpHeaderValFunc _httpHeaderValidationFunc;
113116

114117
bool _runnning;
118+
119+
uint32_t _pingInterval;
120+
uint32_t _pongTimeout;
121+
uint8_t _disconnectTimeoutCount;
115122

116123
bool newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient);
117124

@@ -126,6 +133,8 @@ class WebSocketsServer : protected WebSockets {
126133
#endif
127134

128135
void handleHeader(WSclient_t * client, String * headerLine);
136+
137+
void handleHBPing(WSclient_t * client); // send ping in specified intervals
129138

130139
/**
131140
* called if a non Websocket connection is coming in.

0 commit comments

Comments
 (0)
0