mirror of
https://github.com/Links2004/arduinoWebSockets.git
synced 2025-07-14 15:56:30 +02:00
add setReconnectInterval for the Client
This commit is contained in:
@ -17,71 +17,76 @@
|
|||||||
ESP8266WiFiMulti WiFiMulti;
|
ESP8266WiFiMulti WiFiMulti;
|
||||||
WebSocketsClient webSocket;
|
WebSocketsClient webSocket;
|
||||||
|
|
||||||
|
|
||||||
#define USE_SERIAL Serial1
|
#define USE_SERIAL Serial1
|
||||||
|
|
||||||
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
|
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) {
|
// send message to server when Connected
|
||||||
case WStype_DISCONNECTED:
|
webSocket.sendTXT("Connected");
|
||||||
USE_SERIAL.printf("[WSc] Disconnected!\n");
|
}
|
||||||
break;
|
break;
|
||||||
case WStype_CONNECTED:
|
case WStype_TEXT:
|
||||||
{
|
USE_SERIAL.printf("[WSc] get text: %s\n", payload);
|
||||||
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
|
// send message to server
|
||||||
// webSocket.sendTXT("message here");
|
// webSocket.sendTXT("message here");
|
||||||
break;
|
break;
|
||||||
case WStype_BIN:
|
case WStype_BIN:
|
||||||
USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
|
USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
|
||||||
hexdump(payload, length);
|
hexdump(payload, length);
|
||||||
|
|
||||||
// send data to server
|
// send data to server
|
||||||
// webSocket.sendBIN(payload, length);
|
// webSocket.sendBIN(payload, length);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// USE_SERIAL.begin(921600);
|
// USE_SERIAL.begin(921600);
|
||||||
USE_SERIAL.begin(115200);
|
USE_SERIAL.begin(115200);
|
||||||
|
|
||||||
//Serial.setDebugOutput(true);
|
//Serial.setDebugOutput(true);
|
||||||
USE_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--) {
|
for(uint8_t t = 4; t > 0; t--) {
|
||||||
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
|
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
|
||||||
USE_SERIAL.flush();
|
USE_SERIAL.flush();
|
||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
WiFiMulti.addAP("SSID", "passpasspass");
|
WiFiMulti.addAP("SSID", "passpasspass");
|
||||||
|
|
||||||
//WiFi.disconnect();
|
//WiFi.disconnect();
|
||||||
while(WiFiMulti.run() != WL_CONNECTED) {
|
while(WiFiMulti.run() != WL_CONNECTED) {
|
||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
webSocket.begin("192.168.0.123", 81);
|
// server address, port and URL
|
||||||
//webSocket.setAuthorization("user", "Password"); // HTTP Basic Authorization
|
webSocket.begin("192.168.0.123", 81, "/");
|
||||||
webSocket.onEvent(webSocketEvent);
|
|
||||||
|
// 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() {
|
void loop() {
|
||||||
webSocket.loop();
|
webSocket.loop();
|
||||||
}
|
}
|
||||||
|
@ -34,11 +34,14 @@
|
|||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
|
#ifndef NODEBUG_WEBSOCKETS
|
||||||
#ifdef DEBUG_ESP_PORT
|
#ifdef DEBUG_ESP_PORT
|
||||||
#define DEBUG_WEBSOCKETS(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ )
|
#define DEBUG_WEBSOCKETS(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ )
|
||||||
#else
|
#else
|
||||||
//#define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ )
|
//#define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ )
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifndef DEBUG_WEBSOCKETS
|
#ifndef DEBUG_WEBSOCKETS
|
||||||
#define DEBUG_WEBSOCKETS(...)
|
#define DEBUG_WEBSOCKETS(...)
|
||||||
|
@ -75,6 +75,9 @@ void WebSocketsClient::begin(const char *host, uint16_t port, const char * url,
|
|||||||
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
||||||
asyncConnect();
|
asyncConnect();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
_lastConnectionFail = 0;
|
||||||
|
_reconnectInterval = 500;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebSocketsClient::begin(String host, uint16_t port, String url, String protocol) {
|
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) {
|
void WebSocketsClient::loop(void) {
|
||||||
if(!clientIsConnected(&_client)) {
|
if(!clientIsConnected(&_client)) {
|
||||||
|
// do not flood the server
|
||||||
|
if((millis() - _lastConnectionFail) < _reconnectInterval) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
|
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
|
||||||
if(_client.isSSL) {
|
if(_client.isSSL) {
|
||||||
@ -151,9 +158,11 @@ void WebSocketsClient::loop(void) {
|
|||||||
|
|
||||||
if(_client.tcp->connect(_host.c_str(), _port)) {
|
if(_client.tcp->connect(_host.c_str(), _port)) {
|
||||||
connectedCb();
|
connectedCb();
|
||||||
|
_lastConnectionFail = 0;
|
||||||
} else {
|
} else {
|
||||||
connectFailedCb();
|
connectFailedCb();
|
||||||
delay(10); //some little delay to not flood the server
|
_lastConnectionFail = millis();
|
||||||
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
handleClientData();
|
handleClientData();
|
||||||
@ -284,6 +293,16 @@ void WebSocketsClient::setExtraHeaders(const char * extraHeaders) {
|
|||||||
_client.extraHeaders = 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;
|
ok = false;
|
||||||
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] serverCode is not 101 (%d)\n", client->cCode);
|
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] serverCode is not 101 (%d)\n", client->cCode);
|
||||||
clientDisconnect(client);
|
clientDisconnect(client);
|
||||||
|
_lastConnectionFail = millis();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -634,6 +654,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
|
|||||||
sendHeader(client);
|
sendHeader(client);
|
||||||
} else {
|
} else {
|
||||||
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] no Websocket connection close.\n");
|
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] no Websocket connection close.\n");
|
||||||
|
_lastConnectionFail = millis();
|
||||||
if(clientIsConnected(client)) {
|
if(clientIsConnected(client)) {
|
||||||
client->tcp->write("This is a webSocket client!");
|
client->tcp->write("This is a webSocket client!");
|
||||||
}
|
}
|
||||||
|
@ -83,6 +83,8 @@ class WebSocketsClient: private WebSockets {
|
|||||||
|
|
||||||
void setExtraHeaders(const char * extraHeaders = NULL);
|
void setExtraHeaders(const char * extraHeaders = NULL);
|
||||||
|
|
||||||
|
void setReconnectInterval(unsigned long time);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
String _host;
|
String _host;
|
||||||
uint16_t _port;
|
uint16_t _port;
|
||||||
@ -94,6 +96,9 @@ class WebSocketsClient: private WebSockets {
|
|||||||
|
|
||||||
WebSocketClientEvent _cbEvent;
|
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 messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin);
|
||||||
|
|
||||||
void clientDisconnect(WSclient_t * client);
|
void clientDisconnect(WSclient_t * client);
|
||||||
|
Reference in New Issue
Block a user