Skip to content

Commit 0a4fcd4

Browse files
committed
EIO=4 ping handling #611
1 parent 900d81e commit 0a4fcd4

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/SocketIOclient.cpp

+21-3
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,37 @@ SocketIOclient::~SocketIOclient() {
1818
void SocketIOclient::begin(const char * host, uint16_t port, const char * url, const char * protocol) {
1919
WebSocketsClient::beginSocketIO(host, port, url, protocol);
2020
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
21+
initClient();
2122
}
2223

2324
void SocketIOclient::begin(String host, uint16_t port, String url, String protocol) {
2425
WebSocketsClient::beginSocketIO(host, port, url, protocol);
2526
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
27+
initClient();
2628
}
2729
#if defined(HAS_SSL)
2830
void SocketIOclient::beginSSL(const char * host, uint16_t port, const char * url, const char * protocol) {
2931
WebSocketsClient::beginSocketIOSSL(host, port, url, protocol);
3032
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
33+
initClient();
3134
}
3235

3336
void SocketIOclient::beginSSL(String host, uint16_t port, String url, String protocol) {
3437
WebSocketsClient::beginSocketIOSSL(host, port, url, protocol);
3538
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
39+
initClient();
3640
}
37-
#if !defined(SSL_AXTLS)
41+
#if defined(SSL_BARESSL)
3842
void SocketIOclient::beginSSLWithCA(const char * host, uint16_t port, const char * url, const char * CA_cert, const char * protocol) {
3943
WebSocketsClient::beginSocketIOSSLWithCA(host, port, url, CA_cert, protocol);
4044
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
45+
initClient();
4146
}
4247

4348
void SocketIOclient::beginSSLWithCA(const char * host, uint16_t port, const char * url, BearSSL::X509List * CA_cert, const char * protocol) {
4449
WebSocketsClient::beginSocketIOSSLWithCA(host, port, url, CA_cert, protocol);
4550
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
51+
initClient();
4652
}
4753

4854
void SocketIOclient::setSSLClientCertKey(const char * clientCert, const char * clientPrivateKey) {
@@ -55,6 +61,18 @@ void SocketIOclient::setSSLClientCertKey(BearSSL::X509List * clientCert, BearSSL
5561

5662
#endif
5763
#endif
64+
65+
void SocketIOclient::configureEIOping(bool disableHeartbeat) {
66+
_disableHeartbeat = disableHeartbeat;
67+
}
68+
69+
void SocketIOclient::initClient(void) {
70+
if(_client.cUrl.indexOf("EIO=4") != -1) {
71+
DEBUG_WEBSOCKETS("[wsIOc] found EIO=4 disable EIO ping on client\n");
72+
configureEIOping(true);
73+
}
74+
}
75+
5876
/**
5977
* set callback function
6078
* @param cbEvent SocketIOclientEvent
@@ -148,8 +166,8 @@ bool SocketIOclient::sendEVENT(String & payload) {
148166
void SocketIOclient::loop(void) {
149167
WebSocketsClient::loop();
150168
unsigned long t = millis();
151-
if((t - _lastConnectionFail) > EIO_HEARTBEAT_INTERVAL) {
152-
_lastConnectionFail = t;
169+
if(!_disableHeartbeat && (t - _lastHeartbeat) > EIO_HEARTBEAT_INTERVAL) {
170+
_lastHeartbeat = t;
153171
DEBUG_WEBSOCKETS("[wsIOc] send ping\n");
154172
WebSocketsClient::sendTXT(eIOtype_PING);
155173
}

src/SocketIOclient.h

+5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ class SocketIOclient : protected WebSocketsClient {
7777

7878
void loop(void);
7979

80+
void configureEIOping(bool disableHeartbeat = false);
81+
8082
protected:
83+
bool _disableHeartbeat = false;
8184
uint64_t _lastHeartbeat = 0;
8285
SocketIOclientEvent _cbEvent;
8386
virtual void runIOCbEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) {
@@ -86,6 +89,8 @@ class SocketIOclient : protected WebSocketsClient {
8689
}
8790
}
8891

92+
void initClient(void);
93+
8994
// Handeling events from websocket layer
9095
virtual void runCbEvent(WStype_t type, uint8_t * payload, size_t length) {
9196
handleCbEvent(type, payload, length);

src/WebSocketsClient.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,9 @@ void WebSocketsClient::handleHBPing() {
945945
if(sendPing()) {
946946
_client.lastPing = millis();
947947
_client.pongReceived = false;
948+
} else {
949+
DEBUG_WEBSOCKETS("[WS-Client] sending HB ping failed\n");
950+
WebSockets::clientDisconnect(&_client, 1000);
948951
}
949952
}
950953
}

0 commit comments

Comments
 (0)