Skip to content

Commit 522a67b

Browse files
committed
add setReconnectInterval for the Client
1 parent 29df9c3 commit 522a67b

File tree

4 files changed

+79
-45
lines changed

4 files changed

+79
-45
lines changed

examples/WebSocketClient/WebSocketClient.ino

+49-44
Original file line numberDiff line numberDiff line change
@@ -17,71 +17,76 @@
1717
ESP8266WiFiMulti WiFiMulti;
1818
WebSocketsClient webSocket;
1919

20-
2120
#define USE_SERIAL Serial1
2221

2322
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
2423

24+
switch(type) {
25+
case WStype_DISCONNECTED:
26+
USE_SERIAL.printf("[WSc] Disconnected!\n");
27+
break;
28+
case WStype_CONNECTED: {
29+
USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
2530

26-
switch(type) {
27-
case WStype_DISCONNECTED:
28-
USE_SERIAL.printf("[WSc] Disconnected!\n");
29-
break;
30-
case WStype_CONNECTED:
31-
{
32-
USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
33-
34-
// send message to server when Connected
35-
webSocket.sendTXT("Connected");
36-
}
37-
break;
38-
case WStype_TEXT:
39-
USE_SERIAL.printf("[WSc] get text: %s\n", payload);
31+
// send message to server when Connected
32+
webSocket.sendTXT("Connected");
33+
}
34+
break;
35+
case WStype_TEXT:
36+
USE_SERIAL.printf("[WSc] get text: %s\n", payload);
4037

4138
// send message to server
4239
// webSocket.sendTXT("message here");
43-
break;
44-
case WStype_BIN:
45-
USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
46-
hexdump(payload, length);
40+
break;
41+
case WStype_BIN:
42+
USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
43+
hexdump(payload, length);
4744

48-
// send data to server
49-
// webSocket.sendBIN(payload, length);
50-
break;
51-
}
45+
// send data to server
46+
// webSocket.sendBIN(payload, length);
47+
break;
48+
}
5249

5350
}
5451

5552
void setup() {
56-
// USE_SERIAL.begin(921600);
57-
USE_SERIAL.begin(115200);
53+
// USE_SERIAL.begin(921600);
54+
USE_SERIAL.begin(115200);
55+
56+
//Serial.setDebugOutput(true);
57+
USE_SERIAL.setDebugOutput(true);
58+
59+
USE_SERIAL.println();
60+
USE_SERIAL.println();
61+
USE_SERIAL.println();
62+
63+
for(uint8_t t = 4; t > 0; t--) {
64+
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
65+
USE_SERIAL.flush();
66+
delay(1000);
67+
}
5868

59-
//Serial.setDebugOutput(true);
60-
USE_SERIAL.setDebugOutput(true);
69+
WiFiMulti.addAP("SSID", "passpasspass");
6170

62-
USE_SERIAL.println();
63-
USE_SERIAL.println();
64-
USE_SERIAL.println();
71+
//WiFi.disconnect();
72+
while(WiFiMulti.run() != WL_CONNECTED) {
73+
delay(100);
74+
}
6575

66-
for(uint8_t t = 4; t > 0; t--) {
67-
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
68-
USE_SERIAL.flush();
69-
delay(1000);
70-
}
76+
// server address, port and URL
77+
webSocket.begin("192.168.0.123", 81, "/");
7178

72-
WiFiMulti.addAP("SSID", "passpasspass");
79+
// event handler
80+
webSocket.onEvent(webSocketEvent);
7381

74-
//WiFi.disconnect();
75-
while(WiFiMulti.run() != WL_CONNECTED) {
76-
delay(100);
77-
}
82+
// use HTTP Basic Authorization this is optional remove if not needed
83+
webSocket.setAuthorization("user", "Password");
7884

79-
webSocket.begin("192.168.0.123", 81);
80-
//webSocket.setAuthorization("user", "Password"); // HTTP Basic Authorization
81-
webSocket.onEvent(webSocketEvent);
85+
// try ever 5000 again if connection has failed
86+
webSocket.setReconnectInterval(5000);
8287

8388
}
8489

8590
void loop() {
86-
webSocket.loop();
91+
webSocket.loop();
8792
}

src/WebSockets.h

+3
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@
3434

3535
#include <functional>
3636

37+
#ifndef NODEBUG_WEBSOCKETS
3738
#ifdef DEBUG_ESP_PORT
3839
#define DEBUG_WEBSOCKETS(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ )
3940
#else
4041
//#define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ )
4142
#endif
43+
#endif
44+
4245

4346
#ifndef DEBUG_WEBSOCKETS
4447
#define DEBUG_WEBSOCKETS(...)

src/WebSocketsClient.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ void WebSocketsClient::begin(const char *host, uint16_t port, const char * url,
7575
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
7676
asyncConnect();
7777
#endif
78+
79+
_lastConnectionFail = 0;
80+
_reconnectInterval = 500;
7881
}
7982

8083
void WebSocketsClient::begin(String host, uint16_t port, String url, String protocol) {
@@ -121,6 +124,10 @@ void WebSocketsClient::beginSocketIOSSL(String host, uint16_t port, String url,
121124
*/
122125
void WebSocketsClient::loop(void) {
123126
if(!clientIsConnected(&_client)) {
127+
// do not flood the server
128+
if((millis() - _lastConnectionFail) < _reconnectInterval) {
129+
return;
130+
}
124131

125132
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
126133
if(_client.isSSL) {
@@ -151,9 +158,11 @@ void WebSocketsClient::loop(void) {
151158

152159
if(_client.tcp->connect(_host.c_str(), _port)) {
153160
connectedCb();
161+
_lastConnectionFail = 0;
154162
} else {
155163
connectFailedCb();
156-
delay(10); //some little delay to not flood the server
164+
_lastConnectionFail = millis();
165+
157166
}
158167
} else {
159168
handleClientData();
@@ -284,6 +293,16 @@ void WebSocketsClient::setExtraHeaders(const char * extraHeaders) {
284293
_client.extraHeaders = extraHeaders;
285294
}
286295

296+
297+
/**
298+
* set the reconnect Interval
299+
* how long to wait after a connection initiate failed
300+
* @param time in ms
301+
*/
302+
void WebSocketsClient::setReconnectInterval(unsigned long time) {
303+
_reconnectInterval = time;
304+
}
305+
287306
//#################################################################################
288307
//#################################################################################
289308
//#################################################################################
@@ -604,6 +623,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
604623
ok = false;
605624
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] serverCode is not 101 (%d)\n", client->cCode);
606625
clientDisconnect(client);
626+
_lastConnectionFail = millis();
607627
break;
608628
}
609629
}
@@ -634,6 +654,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
634654
sendHeader(client);
635655
} else {
636656
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] no Websocket connection close.\n");
657+
_lastConnectionFail = millis();
637658
if(clientIsConnected(client)) {
638659
client->tcp->write("This is a webSocket client!");
639660
}

src/WebSocketsClient.h

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class WebSocketsClient: private WebSockets {
8383

8484
void setExtraHeaders(const char * extraHeaders = NULL);
8585

86+
void setReconnectInterval(unsigned long time);
87+
8688
protected:
8789
String _host;
8890
uint16_t _port;
@@ -94,6 +96,9 @@ class WebSocketsClient: private WebSockets {
9496

9597
WebSocketClientEvent _cbEvent;
9698

99+
unsigned long _lastConnectionFail;
100+
unsigned long _reconnectInterval;
101+
97102
void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin);
98103

99104
void clientDisconnect(WSclient_t * client);

0 commit comments

Comments
 (0)