Files
arduinoWebSockets/src/WebSocketsClient.cpp

835 lines
27 KiB
C++
Raw Normal View History

/**
* @file WebSocketsClient.cpp
* @date 20.05.2015
* @author Markus Sattler
*
* Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is part of the WebSockets for Arduino.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "WebSockets.h"
#include "WebSocketsClient.h"
2015-05-23 23:51:32 +02:00
WebSocketsClient::WebSocketsClient() {
2019-06-10 12:57:49 +02:00
_cbEvent = NULL;
_client.num = 0;
_client.cIsClient = true;
2017-07-19 11:12:00 +02:00
_client.extraHeaders = WEBSOCKETS_STRING("Origin: file://");
2015-05-23 23:51:32 +02:00
}
WebSocketsClient::~WebSocketsClient() {
disconnect();
}
/**
* calles to init the Websockets server
*/
2019-06-10 12:57:49 +02:00
void WebSocketsClient::begin(const char * host, uint16_t port, const char * url, const char * protocol) {
2015-05-23 23:51:32 +02:00
_host = host;
_port = port;
2019-05-30 16:32:30 +02:00
#if defined(HAS_SSL)
_fingerprint = "";
2019-06-10 12:57:49 +02:00
_CA_cert = NULL;
2016-01-23 16:27:02 +01:00
#endif
2015-05-23 23:51:32 +02:00
2019-06-10 12:57:49 +02:00
_client.num = 0;
2015-05-23 23:51:32 +02:00
_client.status = WSC_NOT_CONNECTED;
2019-06-10 12:57:49 +02:00
_client.tcp = NULL;
2019-05-30 16:32:30 +02:00
#if defined(HAS_SSL)
2015-12-10 09:36:18 +01:00
_client.isSSL = false;
2019-06-10 12:57:49 +02:00
_client.ssl = NULL;
2015-12-10 09:36:18 +01:00
#endif
2019-06-10 12:57:49 +02:00
_client.cUrl = url;
_client.cCode = 0;
_client.cIsUpgrade = false;
_client.cIsWebsocket = true;
_client.cKey = "";
_client.cAccept = "";
_client.cProtocol = protocol;
_client.cExtensions = "";
_client.cVersion = 0;
_client.base64Authorization = "";
2019-06-10 12:57:49 +02:00
_client.plainAuthorization = "";
_client.isSocketIO = false;
2019-06-10 12:57:49 +02:00
_client.lastPing = 0;
_client.pongReceived = false;
_client.pongTimeoutCount = 0;
2018-10-23 18:01:33 +02:00
2015-12-10 09:36:18 +01:00
#ifdef ESP8266
randomSeed(RANDOM_REG32);
#else
// todo find better seed
randomSeed(millis());
2015-12-10 09:36:18 +01:00
#endif
2019-06-10 12:57:49 +02:00
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
2016-01-24 00:52:23 +01:00
asyncConnect();
2016-01-29 13:16:02 +01:00
#endif
_lastConnectionFail = 0;
2019-06-10 12:57:49 +02:00
_reconnectInterval = 500;
2015-05-23 23:51:32 +02:00
}
void WebSocketsClient::begin(String host, uint16_t port, String url, String protocol) {
begin(host.c_str(), port, url.c_str(), protocol.c_str());
2015-05-23 23:51:32 +02:00
}
2018-05-10 20:12:48 +02:00
void WebSocketsClient::begin(IPAddress host, uint16_t port, const char * url, const char * protocol) {
2018-05-12 11:27:03 +02:00
return begin(host.toString().c_str(), port, url, protocol);
2018-05-10 20:12:48 +02:00
}
2019-05-30 16:32:30 +02:00
#if defined(HAS_SSL)
2019-06-10 12:57:49 +02:00
void WebSocketsClient::beginSSL(const char * host, uint16_t port, const char * url, const char * fingerprint, const char * protocol) {
begin(host, port, url, protocol);
2015-12-24 04:47:11 +01:00
_client.isSSL = true;
2019-06-10 12:57:49 +02:00
_fingerprint = fingerprint;
_CA_cert = NULL;
2015-12-24 04:47:11 +01:00
}
void WebSocketsClient::beginSSL(String host, uint16_t port, String url, String fingerprint, String protocol) {
beginSSL(host.c_str(), port, url.c_str(), fingerprint.c_str(), protocol.c_str());
2015-12-24 04:47:11 +01:00
}
2019-05-30 16:32:30 +02:00
2019-06-10 12:57:49 +02:00
void WebSocketsClient::beginSslWithCA(const char * host, uint16_t port, const char * url, const char * CA_cert, const char * protocol) {
2019-05-30 16:32:30 +02:00
begin(host, port, url, protocol);
_client.isSSL = true;
2019-06-10 12:57:49 +02:00
_fingerprint = "";
_CA_cert = CA_cert;
2019-05-30 16:32:30 +02:00
}
2015-12-10 10:10:06 +01:00
#endif
2019-06-10 12:57:49 +02:00
void WebSocketsClient::beginSocketIO(const char * host, uint16_t port, const char * url, const char * protocol) {
2016-06-06 15:21:13 +03:00
begin(host, port, url, protocol);
_client.isSocketIO = true;
}
void WebSocketsClient::beginSocketIO(String host, uint16_t port, String url, String protocol) {
beginSocketIO(host.c_str(), port, url.c_str(), protocol.c_str());
}
2016-01-24 00:52:23 +01:00
2019-05-30 16:32:30 +02:00
#if defined(HAS_SSL)
2019-06-10 12:57:49 +02:00
void WebSocketsClient::beginSocketIOSSL(const char * host, uint16_t port, const char * url, const char * protocol) {
2017-06-18 12:50:07 +02:00
begin(host, port, url, protocol);
_client.isSocketIO = true;
2019-06-10 12:57:49 +02:00
_client.isSSL = true;
_fingerprint = "";
2017-06-18 12:50:07 +02:00
}
void WebSocketsClient::beginSocketIOSSL(String host, uint16_t port, String url, String protocol) {
beginSocketIOSSL(host.c_str(), port, url.c_str(), protocol.c_str());
}
2019-05-30 16:32:30 +02:00
2019-06-10 12:57:49 +02:00
void WebSocketsClient::beginSocketIOSSLWithCA(const char * host, uint16_t port, const char * url, const char * CA_cert, const char * protocol) {
2019-05-30 16:32:30 +02:00
begin(host, port, url, protocol);
_client.isSocketIO = true;
2019-06-10 12:57:49 +02:00
_client.isSSL = true;
_fingerprint = "";
_CA_cert = CA_cert;
2019-05-30 16:32:30 +02:00
}
2017-06-18 12:50:07 +02:00
#endif
2019-06-10 12:57:49 +02:00
#if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
2015-05-23 23:51:32 +02:00
/**
* called in arduino loop
*/
void WebSocketsClient::loop(void) {
if(!clientIsConnected(&_client)) {
2018-05-12 11:27:03 +02:00
// do not flood the server
if((millis() - _lastConnectionFail) < _reconnectInterval) {
return;
}
2015-12-10 09:36:18 +01:00
2019-06-10 09:29:58 +02:00
#if defined(HAS_SSL)
2015-12-10 09:36:18 +01:00
if(_client.isSSL) {
DEBUG_WEBSOCKETS("[WS-Client] connect wss...\n");
if(_client.ssl) {
delete _client.ssl;
_client.ssl = NULL;
_client.tcp = NULL;
}
2019-06-10 09:29:58 +02:00
_client.ssl = new WEBSOCKETS_NETWORK_SSL_CLASS();
2015-12-10 09:36:18 +01:00
_client.tcp = _client.ssl;
2019-05-30 16:32:30 +02:00
if(_CA_cert) {
DEBUG_WEBSOCKETS("[WS-Client] setting CA certificate");
#if defined(ESP32)
_client.ssl->setCACert(_CA_cert);
#elif defined(ESP8266)
2019-06-10 12:57:49 +02:00
_client.ssl->setCACert((const uint8_t *)_CA_cert, strlen(_CA_cert) + 1);
2019-05-30 16:32:30 +02:00
#else
#error setCACert not implemented
#endif
}
2015-12-10 09:36:18 +01:00
} else {
DEBUG_WEBSOCKETS("[WS-Client] connect ws...\n");
if(_client.tcp) {
delete _client.tcp;
_client.tcp = NULL;
}
2019-06-10 09:29:58 +02:00
_client.tcp = new WEBSOCKETS_NETWORK_CLASS();
2015-12-10 09:36:18 +01:00
}
#else
_client.tcp = new WEBSOCKETS_NETWORK_CLASS();
#endif
if(!_client.tcp) {
DEBUG_WEBSOCKETS("[WS-Client] creating Network class failed!");
return;
}
if(_client.tcp->connect(_host.c_str(), _port)) {
2016-01-24 00:52:23 +01:00
connectedCb();
_lastConnectionFail = 0;
2015-05-23 23:51:32 +02:00
} else {
2016-01-24 00:52:23 +01:00
connectFailedCb();
_lastConnectionFail = millis();
2015-05-23 23:51:32 +02:00
}
} else {
handleClientData();
2019-06-10 12:57:49 +02:00
if(_client.status == WSC_CONNECTED) {
2018-10-23 18:01:33 +02:00
handleHBPing();
handleHBTimeout(&_client);
}
2015-05-23 23:51:32 +02:00
}
}
2016-01-24 00:52:23 +01:00
#endif
2015-05-23 23:51:32 +02:00
/**
* set callback function
* @param cbEvent WebSocketServerEvent
*/
void WebSocketsClient::onEvent(WebSocketClientEvent cbEvent) {
_cbEvent = cbEvent;
}
/**
* send text data to client
* @param num uint8_t client id
* @param payload uint8_t *
* @param length size_t
* @param headerToPayload bool (see sendFrame for more details)
2016-02-20 12:27:19 +01:00
* @return true if ok
2015-05-23 23:51:32 +02:00
*/
2016-02-20 12:27:19 +01:00
bool WebSocketsClient::sendTXT(uint8_t * payload, size_t length, bool headerToPayload) {
2015-05-23 23:51:32 +02:00
if(length == 0) {
2019-06-10 12:57:49 +02:00
length = strlen((const char *)payload);
2015-05-23 23:51:32 +02:00
}
if(clientIsConnected(&_client)) {
return sendFrame(&_client, WSop_text, payload, length, true, headerToPayload);
2015-05-23 23:51:32 +02:00
}
2016-02-20 12:27:19 +01:00
return false;
2015-05-23 23:51:32 +02:00
}
2016-02-20 12:27:19 +01:00
bool WebSocketsClient::sendTXT(const uint8_t * payload, size_t length) {
2019-06-10 12:57:49 +02:00
return sendTXT((uint8_t *)payload, length);
2015-05-23 23:51:32 +02:00
}
2016-02-20 12:27:19 +01:00
bool WebSocketsClient::sendTXT(char * payload, size_t length, bool headerToPayload) {
2019-06-10 12:57:49 +02:00
return sendTXT((uint8_t *)payload, length, headerToPayload);
2015-05-23 23:51:32 +02:00
}
2016-02-20 12:27:19 +01:00
bool WebSocketsClient::sendTXT(const char * payload, size_t length) {
2019-06-10 12:57:49 +02:00
return sendTXT((uint8_t *)payload, length);
2015-05-23 23:51:32 +02:00
}
2016-02-20 12:27:19 +01:00
bool WebSocketsClient::sendTXT(String & payload) {
2019-06-10 12:57:49 +02:00
return sendTXT((uint8_t *)payload.c_str(), payload.length());
2015-05-23 23:51:32 +02:00
}
/**
* send binary data to client
* @param num uint8_t client id
* @param payload uint8_t *
* @param length size_t
* @param headerToPayload bool (see sendFrame for more details)
2016-02-20 12:27:19 +01:00
* @return true if ok
2015-05-23 23:51:32 +02:00
*/
2016-02-20 12:27:19 +01:00
bool WebSocketsClient::sendBIN(uint8_t * payload, size_t length, bool headerToPayload) {
2015-05-23 23:51:32 +02:00
if(clientIsConnected(&_client)) {
return sendFrame(&_client, WSop_binary, payload, length, true, headerToPayload);
2015-05-23 23:51:32 +02:00
}
2016-02-20 12:27:19 +01:00
return false;
2015-05-23 23:51:32 +02:00
}
2016-02-20 12:27:19 +01:00
bool WebSocketsClient::sendBIN(const uint8_t * payload, size_t length) {
2019-06-10 12:57:49 +02:00
return sendBIN((uint8_t *)payload, length);
2015-05-23 23:51:32 +02:00
}
/**
* sends a WS ping to Server
* @param payload uint8_t *
* @param length size_t
* @return true if ping is send out
*/
bool WebSocketsClient::sendPing(uint8_t * payload, size_t length) {
if(clientIsConnected(&_client)) {
2018-10-23 18:01:33 +02:00
bool sent = sendFrame(&_client, WSop_ping, payload, length);
2019-06-10 12:57:49 +02:00
if(sent)
2018-10-23 18:01:33 +02:00
_client.lastPing = millis();
return sent;
}
return false;
}
bool WebSocketsClient::sendPing(String & payload) {
2019-06-10 12:57:49 +02:00
return sendPing((uint8_t *)payload.c_str(), payload.length());
}
2015-05-23 23:51:32 +02:00
/**
* disconnect one client
* @param num uint8_t client id
*/
void WebSocketsClient::disconnect(void) {
if(clientIsConnected(&_client)) {
WebSockets::clientDisconnect(&_client, 1000);
}
}
/**
* set the Authorizatio for the http request
* @param user const char *
* @param password const char *
*/
void WebSocketsClient::setAuthorization(const char * user, const char * password) {
if(user && password) {
String auth = user;
auth += ":";
auth += password;
2019-06-10 12:57:49 +02:00
_client.base64Authorization = base64_encode((uint8_t *)auth.c_str(), auth.length());
}
}
/**
* set the Authorizatio for the http request
* @param auth const char * base64
*/
void WebSocketsClient::setAuthorization(const char * auth) {
if(auth) {
//_client.base64Authorization = auth;
_client.plainAuthorization = auth;
}
}
/**
* set extra headers for the http request;
* separate headers by "\r\n"
* @param extraHeaders const char * extraHeaders
*/
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) {
2018-05-12 11:27:03 +02:00
_reconnectInterval = time;
}
2015-05-23 23:51:32 +02:00
//#################################################################################
//#################################################################################
//#################################################################################
/**
*
* @param client WSclient_t * ptr to the client struct
* @param opcode WSopcode_t
* @param payload uint8_t *
2017-03-06 15:07:20 -05:00
* @param length size_t
2015-05-23 23:51:32 +02:00
*/
2017-03-06 15:07:20 -05:00
void WebSocketsClient::messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin) {
2015-05-23 23:51:32 +02:00
WStype_t type = WStype_ERROR;
UNUSED(client);
2015-05-23 23:51:32 +02:00
switch(opcode) {
case WSop_text:
type = fin ? WStype_TEXT : WStype_FRAGMENT_TEXT_START;
2015-05-23 23:51:32 +02:00
break;
case WSop_binary:
type = fin ? WStype_BIN : WStype_FRAGMENT_BIN_START;
2015-05-23 23:51:32 +02:00
break;
2018-05-12 11:27:03 +02:00
case WSop_continuation:
type = fin ? WStype_FRAGMENT_FIN : WStype_FRAGMENT;
break;
case WSop_ping:
2019-05-30 20:15:03 +02:00
type = WStype_PING;
break;
case WSop_pong:
2019-05-30 20:15:03 +02:00
type = WStype_PONG;
break;
case WSop_close:
default:
break;
2015-05-23 23:51:32 +02:00
}
2017-03-06 15:07:20 -05:00
runCbEvent(type, payload, length);
2015-05-23 23:51:32 +02:00
}
/**
* Disconnect an client
* @param client WSclient_t * ptr to the client struct
*/
void WebSocketsClient::clientDisconnect(WSclient_t * client) {
2016-01-23 16:27:02 +01:00
bool event = false;
2019-06-10 12:57:49 +02:00
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
2015-12-10 09:36:18 +01:00
if(client->isSSL && client->ssl) {
if(client->ssl->connected()) {
client->ssl->flush();
client->ssl->stop();
}
2016-01-23 16:27:02 +01:00
event = true;
2015-12-10 09:36:18 +01:00
delete client->ssl;
client->ssl = NULL;
client->tcp = NULL;
}
#endif
2015-05-23 23:51:32 +02:00
if(client->tcp) {
2015-12-10 09:36:18 +01:00
if(client->tcp->connected()) {
2019-06-10 12:57:49 +02:00
#if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
2015-12-10 09:36:18 +01:00
client->tcp->flush();
2016-01-23 18:34:30 +01:00
#endif
2015-12-10 09:36:18 +01:00
client->tcp->stop();
}
2016-01-23 16:27:02 +01:00
event = true;
2019-06-10 12:57:49 +02:00
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
2016-01-23 18:34:30 +01:00
client->status = WSC_NOT_CONNECTED;
#else
2015-12-10 09:36:18 +01:00
delete client->tcp;
2016-01-23 18:34:30 +01:00
#endif
2015-12-10 09:36:18 +01:00
client->tcp = NULL;
2015-05-23 23:51:32 +02:00
}
2019-06-10 12:57:49 +02:00
client->cCode = 0;
client->cKey = "";
client->cAccept = "";
client->cVersion = 0;
client->cIsUpgrade = false;
2015-05-23 23:51:32 +02:00
client->cIsWebsocket = false;
2019-06-10 12:57:49 +02:00
client->cSessionId = "";
2015-05-23 23:51:32 +02:00
client->status = WSC_NOT_CONNECTED;
DEBUG_WEBSOCKETS("[WS-Client] client disconnected.\n");
2016-01-23 16:27:02 +01:00
if(event) {
runCbEvent(WStype_DISCONNECTED, NULL, 0);
}
2015-05-23 23:51:32 +02:00
}
/**
* get client state
* @param client WSclient_t * ptr to the client struct
* @return true = conneted
*/
bool WebSocketsClient::clientIsConnected(WSclient_t * client) {
2015-12-10 09:36:18 +01:00
if(!client->tcp) {
return false;
}
if(client->tcp->connected()) {
2015-12-05 11:43:03 +01:00
if(client->status != WSC_NOT_CONNECTED) {
return true;
}
} else {
// client lost
if(client->status != WSC_NOT_CONNECTED) {
DEBUG_WEBSOCKETS("[WS-Client] connection lost.\n");
// do cleanup
clientDisconnect(client);
}
2015-05-23 23:51:32 +02:00
}
2015-12-10 09:36:18 +01:00
if(client->tcp) {
// do cleanup
clientDisconnect(client);
}
2015-05-23 23:51:32 +02:00
return false;
}
2019-06-10 12:57:49 +02:00
#if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
2015-05-23 23:51:32 +02:00
/**
* Handel incomming data from Client
*/
void WebSocketsClient::handleClientData(void) {
2015-12-10 09:36:18 +01:00
int len = _client.tcp->available();
2015-05-23 23:51:32 +02:00
if(len > 0) {
switch(_client.status) {
2018-05-12 11:27:03 +02:00
case WSC_HEADER: {
String headerLine = _client.tcp->readStringUntil('\n');
2016-01-23 16:27:02 +01:00
handleHeader(&_client, &headerLine);
2019-06-10 12:57:49 +02:00
} break;
2015-05-23 23:51:32 +02:00
case WSC_CONNECTED:
WebSockets::handleWebsocket(&_client);
break;
default:
WebSockets::clientDisconnect(&_client, 1002);
break;
}
}
2019-06-10 12:57:49 +02:00
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
2015-05-23 23:51:32 +02:00
delay(0);
#endif
}
2016-01-23 16:27:02 +01:00
#endif
/**
* send the WebSocket header to Server
* @param client WSclient_t * ptr to the client struct
*/
void WebSocketsClient::sendHeader(WSclient_t * client) {
2018-05-12 11:27:03 +02:00
static const char * NEW_LINE = "\r\n";
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header...\n");
uint8_t randomKey[16] = { 0 };
for(uint8_t i = 0; i < sizeof(randomKey); i++) {
randomKey[i] = random(0xFF);
}
client->cKey = base64_encode(&randomKey[0], 16);
#ifndef NODEBUG_WEBSOCKETS
unsigned long start = micros();
#endif
2016-06-06 15:21:13 +03:00
String handshake;
bool ws_header = true;
2019-06-10 12:57:49 +02:00
String url = client->cUrl;
if(client->isSocketIO) {
2018-05-12 11:27:03 +02:00
if(client->cSessionId.length() == 0) {
url += WEBSOCKETS_STRING("&transport=polling");
ws_header = false;
} else {
url += WEBSOCKETS_STRING("&transport=websocket&sid=");
url += client->cSessionId;
}
}
2018-05-12 11:27:03 +02:00
handshake = WEBSOCKETS_STRING("GET ");
2019-06-10 12:57:49 +02:00
handshake += url + WEBSOCKETS_STRING(
" HTTP/1.1\r\n"
"Host: ");
2018-05-12 11:27:03 +02:00
handshake += _host + ":" + _port + NEW_LINE;
if(ws_header) {
2019-06-10 12:57:49 +02:00
handshake += WEBSOCKETS_STRING(
"Connection: Upgrade\r\n"
"Upgrade: websocket\r\n"
"Sec-WebSocket-Version: 13\r\n"
"Sec-WebSocket-Key: ");
2018-05-12 11:27:03 +02:00
handshake += client->cKey + NEW_LINE;
if(client->cProtocol.length() > 0) {
handshake += WEBSOCKETS_STRING("Sec-WebSocket-Protocol: ");
handshake += client->cProtocol + NEW_LINE;
}
if(client->cExtensions.length() > 0) {
handshake += WEBSOCKETS_STRING("Sec-WebSocket-Extensions: ");
handshake += client->cExtensions + NEW_LINE;
}
} else {
handshake += WEBSOCKETS_STRING("Connection: keep-alive\r\n");
}
// add extra headers; by default this includes "Origin: file://"
if(client->extraHeaders) {
handshake += client->extraHeaders + NEW_LINE;
}
handshake += WEBSOCKETS_STRING("User-Agent: arduino-WebSocket-Client\r\n");
if(client->base64Authorization.length() > 0) {
handshake += WEBSOCKETS_STRING("Authorization: Basic ");
handshake += client->base64Authorization + NEW_LINE;
}
if(client->plainAuthorization.length() > 0) {
handshake += WEBSOCKETS_STRING("Authorization: ");
handshake += client->plainAuthorization + NEW_LINE;
}
handshake += NEW_LINE;
2019-06-10 12:57:49 +02:00
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] handshake %s", (uint8_t *)handshake.c_str());
write(client, (uint8_t *)handshake.c_str(), handshake.length());
2019-06-10 12:57:49 +02:00
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
2018-05-12 11:27:03 +02:00
client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsClient::handleHeader, this, client, &(client->cHttpLine)));
2016-01-23 16:27:02 +01:00
#endif
2018-05-10 01:16:39 +03:00
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header... Done (%luus).\n", (micros() - start));
2015-05-23 23:51:32 +02:00
}
/**
* handle the WebSocket header reading
* @param client WSclient_t * ptr to the client struct
*/
2016-01-23 16:27:02 +01:00
void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
2019-06-10 12:57:49 +02:00
headerLine->trim(); // remove \r
2018-05-12 11:27:03 +02:00
if(headerLine->length() > 0) {
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] RX: %s\n", headerLine->c_str());
if(headerLine->startsWith(WEBSOCKETS_STRING("HTTP/1."))) {
// "HTTP/1.1 101 Switching Protocols"
client->cCode = headerLine->substring(9, headerLine->indexOf(' ', 9)).toInt();
2019-05-30 16:52:20 +02:00
} else if(headerLine->indexOf(':') >= 0) {
2019-06-10 12:57:49 +02:00
String headerName = headerLine->substring(0, headerLine->indexOf(':'));
2018-05-12 11:27:03 +02:00
String headerValue = headerLine->substring(headerLine->indexOf(':') + 1);
// remove space in the beginning (RFC2616)
if(headerValue[0] == ' ') {
headerValue.remove(0, 1);
}
if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Connection"))) {
if(headerValue.equalsIgnoreCase(WEBSOCKETS_STRING("upgrade"))) {
client->cIsUpgrade = true;
}
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Upgrade"))) {
if(headerValue.equalsIgnoreCase(WEBSOCKETS_STRING("websocket"))) {
client->cIsWebsocket = true;
}
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Accept"))) {
client->cAccept = headerValue;
2019-06-10 12:57:49 +02:00
client->cAccept.trim(); // see rfc6455
2018-05-12 11:27:03 +02:00
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Protocol"))) {
client->cProtocol = headerValue;
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Extensions"))) {
client->cExtensions = headerValue;
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Version"))) {
client->cVersion = headerValue.toInt();
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Set-Cookie"))) {
if(headerValue.indexOf(WEBSOCKETS_STRING("HttpOnly")) > -1) {
client->cSessionId = headerValue.substring(headerValue.indexOf('=') + 1, headerValue.indexOf(";"));
} else {
client->cSessionId = headerValue.substring(headerValue.indexOf('=') + 1);
}
}
} else {
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Header error (%s)\n", headerLine->c_str());
}
2015-05-23 23:51:32 +02:00
2016-01-23 16:27:02 +01:00
(*headerLine) = "";
2019-06-10 12:57:49 +02:00
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
2016-01-23 16:27:02 +01:00
client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsClient::handleHeader, this, client, &(client->cHttpLine)));
#endif
2015-05-23 23:51:32 +02:00
} else {
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Header read fin.\n");
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Client settings:\n");
2015-05-23 23:51:32 +02:00
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] - cURL: %s\n", client->cUrl.c_str());
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] - cKey: %s\n", client->cKey.c_str());
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Server header:\n");
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] - cCode: %d\n", client->cCode);
2015-05-23 23:51:32 +02:00
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] - cIsUpgrade: %d\n", client->cIsUpgrade);
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] - cIsWebsocket: %d\n", client->cIsWebsocket);
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] - cAccept: %s\n", client->cAccept.c_str());
2015-05-23 23:51:32 +02:00
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] - cProtocol: %s\n", client->cProtocol.c_str());
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] - cExtensions: %s\n", client->cExtensions.c_str());
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] - cVersion: %d\n", client->cVersion);
2016-06-06 15:21:13 +03:00
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] - cSessionId: %s\n", client->cSessionId.c_str());
2015-05-23 23:51:32 +02:00
bool ok = (client->cIsUpgrade && client->cIsWebsocket);
if(ok) {
switch(client->cCode) {
2019-06-10 12:57:49 +02:00
case 101: ///< Switching Protocols
break;
2016-06-06 15:21:13 +03:00
case 200:
if(client->isSocketIO) {
break;
}
2019-06-10 12:57:49 +02:00
case 403: ///< Forbidden
// todo handle login
2019-06-10 12:57:49 +02:00
default: ///< Server dont unterstand requrst
ok = false;
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] serverCode is not 101 (%d)\n", client->cCode);
clientDisconnect(client);
_lastConnectionFail = millis();
break;
2015-05-23 23:51:32 +02:00
}
}
if(ok) {
if(client->cAccept.length() == 0) {
2015-05-23 23:51:32 +02:00
ok = false;
} else {
// generate Sec-WebSocket-Accept key for check
String sKey = acceptKey(client->cKey);
if(sKey != client->cAccept) {
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Sec-WebSocket-Accept is wrong\n");
ok = false;
}
2015-05-23 23:51:32 +02:00
}
}
if(ok) {
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Websocket connection init done.\n");
2016-01-23 16:27:02 +01:00
headerDone(client);
2015-05-23 23:51:32 +02:00
2019-06-10 12:57:49 +02:00
runCbEvent(WStype_CONNECTED, (uint8_t *)client->cUrl.c_str(), client->cUrl.length());
2018-05-12 11:27:03 +02:00
} else if(clientIsConnected(client) && client->isSocketIO && client->cSessionId.length() > 0) {
sendHeader(client);
} else {
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] no Websocket connection close.\n");
_lastConnectionFail = millis();
if(clientIsConnected(client)) {
write(client, "This is a webSocket client!");
}
clientDisconnect(client);
}
}
2015-05-23 23:51:32 +02:00
}
2016-01-24 00:52:23 +01:00
void WebSocketsClient::connectedCb() {
DEBUG_WEBSOCKETS("[WS-Client] connected to %s:%u.\n", _host.c_str(), _port);
2019-06-10 12:57:49 +02:00
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
2016-01-24 00:52:23 +01:00
_client.tcp->onDisconnect(std::bind([](WebSocketsClient * c, AsyncTCPbuffer * obj, WSclient_t * client) -> bool {
2019-06-10 12:57:49 +02:00
DEBUG_WEBSOCKETS("[WS-Server][%d] Disconnect client\n", client->num);
client->status = WSC_NOT_CONNECTED;
client->tcp = NULL;
2016-01-24 00:52:23 +01:00
2019-06-10 12:57:49 +02:00
// reconnect
c->asyncConnect();
2016-01-24 00:52:23 +01:00
2019-06-10 12:57:49 +02:00
return true;
},
this, std::placeholders::_1, &_client));
2016-01-24 00:52:23 +01:00
#endif
_client.status = WSC_HEADER;
2019-06-10 12:57:49 +02:00
#if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
2016-01-24 00:52:23 +01:00
// set Timeout for readBytesUntil and readStringUntil
_client.tcp->setTimeout(WEBSOCKETS_TCP_TIMEOUT);
#endif
2019-06-10 12:57:49 +02:00
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
2016-01-24 00:52:23 +01:00
_client.tcp->setNoDelay(true);
2019-05-30 16:32:30 +02:00
#endif
2016-01-24 00:52:23 +01:00
2019-05-30 16:32:30 +02:00
#if defined(HAS_SSL)
2016-01-24 00:52:23 +01:00
if(_client.isSSL && _fingerprint.length()) {
if(!_client.ssl->verify(_fingerprint.c_str(), _host.c_str())) {
DEBUG_WEBSOCKETS("[WS-Client] certificate mismatch\n");
WebSockets::clientDisconnect(&_client, 1000);
return;
}
} else if(_client.isSSL && !_CA_cert) {
#if defined(wificlientbearssl_h) && !defined(USING_AXTLS)
_client.ssl->setInsecure();
#endif
2016-01-24 00:52:23 +01:00
}
#endif
// send Header to Server
sendHeader(&_client);
}
void WebSocketsClient::connectFailedCb() {
DEBUG_WEBSOCKETS("[WS-Client] connection to %s:%u Failed\n", _host.c_str(), _port);
2016-01-24 00:52:23 +01:00
}
2019-06-10 12:57:49 +02:00
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
2016-01-24 00:52:23 +01:00
void WebSocketsClient::asyncConnect() {
2016-01-29 13:16:02 +01:00
DEBUG_WEBSOCKETS("[WS-Client] asyncConnect...\n");
2016-01-24 00:52:23 +01:00
2016-01-29 13:16:02 +01:00
AsyncClient * tcpclient = new AsyncClient();
2016-01-24 00:52:23 +01:00
if(!tcpclient) {
2016-01-29 13:16:02 +01:00
DEBUG_WEBSOCKETS("[WS-Client] creating AsyncClient class failed!\n");
2016-01-24 00:52:23 +01:00
return;
}
2019-06-10 12:57:49 +02:00
tcpclient->onDisconnect([](void * obj, AsyncClient * c) {
c->free();
delete c;
});
2016-01-29 13:16:02 +01:00
2019-06-10 12:57:49 +02:00
tcpclient->onConnect(std::bind([](WebSocketsClient * ws, AsyncClient * tcp) {
ws->_client.tcp = new AsyncTCPbuffer(tcp);
if(!ws->_client.tcp) {
DEBUG_WEBSOCKETS("[WS-Client] creating Network class failed!\n");
ws->connectFailedCb();
return;
}
ws->connectedCb();
},
this, std::placeholders::_2));
2016-01-24 00:52:23 +01:00
2019-06-10 12:57:49 +02:00
tcpclient->onError(std::bind([](WebSocketsClient * ws, AsyncClient * tcp) {
ws->connectFailedCb();
2016-01-24 00:52:23 +01:00
2019-06-10 12:57:49 +02:00
// reconnect
ws->asyncConnect();
},
this, std::placeholders::_2));
2016-01-24 00:52:23 +01:00
if(!tcpclient->connect(_host.c_str(), _port)) {
connectFailedCb();
delete tcpclient;
}
}
#endif
2018-10-23 18:01:33 +02:00
/**
* send heartbeat ping to server in set intervals
2019-06-10 12:57:49 +02:00
*/
void WebSocketsClient::handleHBPing() {
if(_client.pingInterval == 0)
return;
2018-10-23 18:01:33 +02:00
uint32_t pi = millis() - _client.lastPing;
2019-06-10 12:57:49 +02:00
if(pi > _client.pingInterval) {
2018-10-23 18:01:33 +02:00
DEBUG_WEBSOCKETS("[WS-Client] sending HB ping\n");
2019-06-10 12:57:49 +02:00
if(sendPing()) {
_client.lastPing = millis();
2018-10-23 18:01:33 +02:00
_client.pongReceived = false;
}
}
}
/**
* enable ping/pong heartbeat process
* @param pingInterval uint32_t how often ping will be sent
* @param pongTimeout uint32_t millis after which pong should timout if not received
* @param disconnectTimeoutCount uint8_t how many timeouts before disconnect, 0=> do not disconnect
*/
2019-06-10 12:57:49 +02:00
void WebSocketsClient::enableHeartbeat(uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount) {
2018-10-23 18:01:33 +02:00
WebSockets::enableHeartbeat(&_client, pingInterval, pongTimeout, disconnectTimeoutCount);
}
/**
* disable ping/pong heartbeat process
*/
2019-06-10 12:57:49 +02:00
void WebSocketsClient::disableHeartbeat() {
2018-10-23 18:01:33 +02:00
_client.pingInterval = 0;
2019-05-30 16:32:30 +02:00
}