From 522a67bc1b4ec0e56cb7e86d4d0bd780ccfa3dff Mon Sep 17 00:00:00 2001 From: Links Date: Sat, 19 Aug 2017 21:16:07 +0200 Subject: [PATCH] add setReconnectInterval for the Client --- examples/WebSocketClient/WebSocketClient.ino | 93 +++++++++++--------- src/WebSockets.h | 3 + src/WebSocketsClient.cpp | 23 ++++- src/WebSocketsClient.h | 5 ++ 4 files changed, 79 insertions(+), 45 deletions(-) diff --git a/examples/WebSocketClient/WebSocketClient.ino b/examples/WebSocketClient/WebSocketClient.ino index 5a344fd..3ce8498 100644 --- a/examples/WebSocketClient/WebSocketClient.ino +++ b/examples/WebSocketClient/WebSocketClient.ino @@ -17,71 +17,76 @@ ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; - #define USE_SERIAL Serial1 void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { + switch(type) { + case WStype_DISCONNECTED: + USE_SERIAL.printf("[WSc] Disconnected!\n"); + break; + case WStype_CONNECTED: { + USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload); - switch(type) { - case WStype_DISCONNECTED: - USE_SERIAL.printf("[WSc] Disconnected!\n"); - break; - case WStype_CONNECTED: - { - USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload); - - // send message to server when Connected - webSocket.sendTXT("Connected"); - } - break; - case WStype_TEXT: - USE_SERIAL.printf("[WSc] get text: %s\n", payload); + // send message to server when Connected + webSocket.sendTXT("Connected"); + } + break; + case WStype_TEXT: + USE_SERIAL.printf("[WSc] get text: %s\n", payload); // send message to server // webSocket.sendTXT("message here"); - break; - case WStype_BIN: - USE_SERIAL.printf("[WSc] get binary length: %u\n", length); - hexdump(payload, length); + break; + case WStype_BIN: + USE_SERIAL.printf("[WSc] get binary length: %u\n", length); + hexdump(payload, length); - // send data to server - // webSocket.sendBIN(payload, length); - break; - } + // send data to server + // webSocket.sendBIN(payload, length); + break; + } } void setup() { - // USE_SERIAL.begin(921600); - USE_SERIAL.begin(115200); + // USE_SERIAL.begin(921600); + USE_SERIAL.begin(115200); - //Serial.setDebugOutput(true); - USE_SERIAL.setDebugOutput(true); + //Serial.setDebugOutput(true); + USE_SERIAL.setDebugOutput(true); - USE_SERIAL.println(); - USE_SERIAL.println(); - USE_SERIAL.println(); + USE_SERIAL.println(); + USE_SERIAL.println(); + USE_SERIAL.println(); - for(uint8_t t = 4; t > 0; t--) { - USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t); - USE_SERIAL.flush(); - delay(1000); - } + for(uint8_t t = 4; t > 0; t--) { + USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t); + USE_SERIAL.flush(); + delay(1000); + } - WiFiMulti.addAP("SSID", "passpasspass"); + WiFiMulti.addAP("SSID", "passpasspass"); - //WiFi.disconnect(); - while(WiFiMulti.run() != WL_CONNECTED) { - delay(100); - } + //WiFi.disconnect(); + while(WiFiMulti.run() != WL_CONNECTED) { + delay(100); + } - webSocket.begin("192.168.0.123", 81); - //webSocket.setAuthorization("user", "Password"); // HTTP Basic Authorization - webSocket.onEvent(webSocketEvent); + // server address, port and URL + webSocket.begin("192.168.0.123", 81, "/"); + + // event handler + webSocket.onEvent(webSocketEvent); + + // use HTTP Basic Authorization this is optional remove if not needed + webSocket.setAuthorization("user", "Password"); + + // try ever 5000 again if connection has failed + webSocket.setReconnectInterval(5000); } void loop() { - webSocket.loop(); + webSocket.loop(); } diff --git a/src/WebSockets.h b/src/WebSockets.h index 41cbe7d..cca3ae1 100644 --- a/src/WebSockets.h +++ b/src/WebSockets.h @@ -34,11 +34,14 @@ #include +#ifndef NODEBUG_WEBSOCKETS #ifdef DEBUG_ESP_PORT #define DEBUG_WEBSOCKETS(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ ) #else //#define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ ) #endif +#endif + #ifndef DEBUG_WEBSOCKETS #define DEBUG_WEBSOCKETS(...) diff --git a/src/WebSocketsClient.cpp b/src/WebSocketsClient.cpp index 3faae99..ab1bf66 100644 --- a/src/WebSocketsClient.cpp +++ b/src/WebSocketsClient.cpp @@ -75,6 +75,9 @@ void WebSocketsClient::begin(const char *host, uint16_t port, const char * url, #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) asyncConnect(); #endif + + _lastConnectionFail = 0; + _reconnectInterval = 500; } 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, */ void WebSocketsClient::loop(void) { if(!clientIsConnected(&_client)) { + // do not flood the server + if((millis() - _lastConnectionFail) < _reconnectInterval) { + return; + } #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) if(_client.isSSL) { @@ -151,9 +158,11 @@ void WebSocketsClient::loop(void) { if(_client.tcp->connect(_host.c_str(), _port)) { connectedCb(); + _lastConnectionFail = 0; } else { connectFailedCb(); - delay(10); //some little delay to not flood the server + _lastConnectionFail = millis(); + } } else { handleClientData(); @@ -284,6 +293,16 @@ void WebSocketsClient::setExtraHeaders(const char * extraHeaders) { _client.extraHeaders = extraHeaders; } + +/** + * set the reconnect Interval + * how long to wait after a connection initiate failed + * @param time in ms + */ +void WebSocketsClient::setReconnectInterval(unsigned long time) { + _reconnectInterval = time; +} + //################################################################################# //################################################################################# //################################################################################# @@ -604,6 +623,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) { ok = false; DEBUG_WEBSOCKETS("[WS-Client][handleHeader] serverCode is not 101 (%d)\n", client->cCode); clientDisconnect(client); + _lastConnectionFail = millis(); break; } } @@ -634,6 +654,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) { sendHeader(client); } else { DEBUG_WEBSOCKETS("[WS-Client][handleHeader] no Websocket connection close.\n"); + _lastConnectionFail = millis(); if(clientIsConnected(client)) { client->tcp->write("This is a webSocket client!"); } diff --git a/src/WebSocketsClient.h b/src/WebSocketsClient.h index 7390a76..c1f5b0c 100644 --- a/src/WebSocketsClient.h +++ b/src/WebSocketsClient.h @@ -83,6 +83,8 @@ class WebSocketsClient: private WebSockets { void setExtraHeaders(const char * extraHeaders = NULL); + void setReconnectInterval(unsigned long time); + protected: String _host; uint16_t _port; @@ -94,6 +96,9 @@ class WebSocketsClient: private WebSockets { WebSocketClientEvent _cbEvent; + unsigned long _lastConnectionFail; + unsigned long _reconnectInterval; + void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin); void clientDisconnect(WSclient_t * client);