mirror of
https://github.com/Links2004/arduinoWebSockets.git
synced 2025-06-29 08:51:00 +02:00
Compare commits
39 Commits
Author | SHA1 | Date | |
---|---|---|---|
f40a390ab1 | |||
b6c27f74cd | |||
c64a082270 | |||
522a67bc1b | |||
29df9c30f7 | |||
0ca020bbd2 | |||
cc3ce02c67 | |||
96c266101e | |||
64c8908b05 | |||
87c6c80c67 | |||
adb255bda0 | |||
d0ab6c4fd1 | |||
fcb623ce91 | |||
669f0b489e | |||
32cb052f23 | |||
42ab3168c5 | |||
86d2e2400a | |||
ad07f3c665 | |||
a4533a028c | |||
ae3dd103a1 | |||
40f84c19f4 | |||
f68d9d8030 | |||
c911776860 | |||
adb52b11e9 | |||
81e567b248 | |||
1afe317c7d | |||
177ec8239d | |||
e675c7590e | |||
615926ae1e | |||
acd0a603eb | |||
26140be6c9 | |||
3b1dabbe1e | |||
55bc7db7ad | |||
1a6f46c67d | |||
210f2e8fa1 | |||
34a2d282e4 | |||
e93a323e56 | |||
6da0bc97e8 | |||
ae2ba7effe |
37
.travis.yml
Normal file
37
.travis.yml
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
sudo: false
|
||||||
|
language: bash
|
||||||
|
os:
|
||||||
|
- linux
|
||||||
|
|
||||||
|
script:
|
||||||
|
- /sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16
|
||||||
|
- sleep 3
|
||||||
|
- export DISPLAY=:1.0
|
||||||
|
- wget http://downloads.arduino.cc/arduino-1.6.5-linux64.tar.xz
|
||||||
|
- tar xf arduino-1.6.5-linux64.tar.xz
|
||||||
|
- mv arduino-1.6.5 $HOME/arduino_ide
|
||||||
|
- export PATH="$HOME/arduino_ide:$PATH"
|
||||||
|
- which arduino
|
||||||
|
- mkdir -p $HOME/Arduino/libraries
|
||||||
|
- cp -r $TRAVIS_BUILD_DIR $HOME/Arduino/libraries/arduinoWebSockets
|
||||||
|
- cd $HOME/arduino_ide/hardware
|
||||||
|
- mkdir esp8266com
|
||||||
|
- cd esp8266com
|
||||||
|
- git clone https://github.com/esp8266/Arduino.git esp8266
|
||||||
|
- cd esp8266/tools
|
||||||
|
- python get.py
|
||||||
|
- source $TRAVIS_BUILD_DIR/travis/common.sh
|
||||||
|
- arduino --board esp8266com:esp8266:generic --save-prefs
|
||||||
|
- arduino --get-pref sketchbook.path
|
||||||
|
- build_sketches arduino $HOME/Arduino/libraries/arduinoWebSockets esp8266
|
||||||
|
|
||||||
|
notifications:
|
||||||
|
email:
|
||||||
|
on_success: change
|
||||||
|
on_failure: change
|
||||||
|
webhooks:
|
||||||
|
urls:
|
||||||
|
- https://webhooks.gitter.im/e/1aa78fbe15080b0c2e37
|
||||||
|
on_success: change # options: [always|never|change] default: always
|
||||||
|
on_failure: always # options: [always|never|change] default: always
|
||||||
|
on_start: false # default: false
|
41
README.md
41
README.md
@ -1,4 +1,4 @@
|
|||||||
WebSocket Server and Client for Arduino
|
WebSocket Server and Client for Arduino [](https://travis-ci.org/Links2004/arduinoWebSockets)
|
||||||
===========================================
|
===========================================
|
||||||
|
|
||||||
a WebSocket Server and Client for Arduino based on RFC6455.
|
a WebSocket Server and Client for Arduino based on RFC6455.
|
||||||
@ -10,14 +10,13 @@ a WebSocket Server and Client for Arduino based on RFC6455.
|
|||||||
- connection close
|
- connection close
|
||||||
- ping
|
- ping
|
||||||
- pong
|
- pong
|
||||||
|
|
||||||
##### Not supported features of RFC6455 #####
|
|
||||||
- continuation frame
|
- continuation frame
|
||||||
|
|
||||||
##### Limitations #####
|
##### Limitations #####
|
||||||
- max input length is limited to the ram size and the ```WEBSOCKETS_MAX_DATA_SIZE``` define
|
- max input length is limited to the ram size and the ```WEBSOCKETS_MAX_DATA_SIZE``` define
|
||||||
- max output length has no limit (the hardware is the limit)
|
- max output length has no limit (the hardware is the limit)
|
||||||
- Client send big frames with mask 0x00000000 (on AVR all frames)
|
- Client send big frames with mask 0x00000000 (on AVR all frames)
|
||||||
|
- continuation frame reassembly need to be handled in the application code
|
||||||
|
|
||||||
##### Limitations for Async #####
|
##### Limitations for Async #####
|
||||||
- Functions called from within the context of the websocket event might not honor `yield()` and/or `delay()`. See [this issue](https://github.com/Links2004/arduinoWebSockets/issues/58#issuecomment-192376395) for more info and a potential workaround.
|
- Functions called from within the context of the websocket event might not honor `yield()` and/or `delay()`. See [this issue](https://github.com/Links2004/arduinoWebSockets/issues/58#issuecomment-192376395) for more info and a potential workaround.
|
||||||
@ -26,6 +25,7 @@ a WebSocket Server and Client for Arduino based on RFC6455.
|
|||||||
##### Supported Hardware #####
|
##### Supported Hardware #####
|
||||||
- ESP8266 [Arduino for ESP8266](https://github.com/Links2004/Arduino)
|
- ESP8266 [Arduino for ESP8266](https://github.com/Links2004/Arduino)
|
||||||
- ESP31B
|
- ESP31B
|
||||||
|
- Particle with STM32 ARM Cortex M3
|
||||||
- ATmega328 with Ethernet Shield (ATmega branch)
|
- ATmega328 with Ethernet Shield (ATmega branch)
|
||||||
- ATmega328 with enc28j60 (ATmega branch)
|
- ATmega328 with enc28j60 (ATmega branch)
|
||||||
- ATmega2560 with Ethernet Shield (ATmega branch)
|
- ATmega2560 with Ethernet Shield (ATmega branch)
|
||||||
@ -52,9 +52,38 @@ The mode can be activated in the ```WebSockets.h``` (see WEBSOCKETS_NETWORK_TYPE
|
|||||||
|
|
||||||
[ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP) libary is required.
|
[ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP) libary is required.
|
||||||
|
|
||||||
### Support for Particle devices ###
|
|
||||||
- ESP.getFreeHeap() replaced by macro GET_FREE_HEAP, defined by the type of device (currently only for ESP and STM32-based/Particle devices).
|
### High Level Client API ###
|
||||||
- Use Particle's TCPClient and TCPServer classes instead of Arduino's.
|
|
||||||
|
- `begin` : Initiate connection sequence to the websocket host.
|
||||||
|
```
|
||||||
|
void begin(const char *host, uint16_t port, const char * url = "/", const char * protocol = "arduino");
|
||||||
|
void begin(String host, uint16_t port, String url = "/", String protocol = "arduino");
|
||||||
|
```
|
||||||
|
- `onEvent`: Callback to handle for websocket events
|
||||||
|
|
||||||
|
```
|
||||||
|
void onEvent(WebSocketClientEvent cbEvent);
|
||||||
|
```
|
||||||
|
|
||||||
|
- `WebSocketClientEvent`: Handler for websocket events
|
||||||
|
```
|
||||||
|
void (*WebSocketClientEvent)(WStype_t type, uint8_t * payload, size_t length)
|
||||||
|
```
|
||||||
|
Where `WStype_t type` is defined as:
|
||||||
|
```
|
||||||
|
typedef enum {
|
||||||
|
WStype_ERROR,
|
||||||
|
WStype_DISCONNECTED,
|
||||||
|
WStype_CONNECTED,
|
||||||
|
WStype_TEXT,
|
||||||
|
WStype_BIN,
|
||||||
|
WStype_FRAGMENT_TEXT_START,
|
||||||
|
WStype_FRAGMENT_BIN_START,
|
||||||
|
WStype_FRAGMENT,
|
||||||
|
WStype_FRAGMENT_FIN,
|
||||||
|
} WStype_t;
|
||||||
|
```
|
||||||
|
|
||||||
### Issues ###
|
### Issues ###
|
||||||
Submit issues to: https://github.com/Links2004/arduinoWebSockets/issues
|
Submit issues to: https://github.com/Links2004/arduinoWebSockets/issues
|
||||||
|
0
examples/ParticleWebSocketClient/.esp8266.skip
Normal file
0
examples/ParticleWebSocketClient/.esp8266.skip
Normal file
@ -17,18 +17,15 @@
|
|||||||
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 lenght) {
|
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
|
||||||
|
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case WStype_DISCONNECTED:
|
case WStype_DISCONNECTED:
|
||||||
USE_SERIAL.printf("[WSc] Disconnected!\n");
|
USE_SERIAL.printf("[WSc] Disconnected!\n");
|
||||||
break;
|
break;
|
||||||
case WStype_CONNECTED:
|
case WStype_CONNECTED: {
|
||||||
{
|
|
||||||
USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
|
USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
|
||||||
|
|
||||||
// send message to server when Connected
|
// send message to server when Connected
|
||||||
@ -42,11 +39,11 @@ void webSocketEvent(WStype_t type, uint8_t * payload, size_t lenght) {
|
|||||||
// webSocket.sendTXT("message here");
|
// webSocket.sendTXT("message here");
|
||||||
break;
|
break;
|
||||||
case WStype_BIN:
|
case WStype_BIN:
|
||||||
USE_SERIAL.printf("[WSc] get binary lenght: %u\n", lenght);
|
USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
|
||||||
hexdump(payload, lenght);
|
hexdump(payload, length);
|
||||||
|
|
||||||
// send data to server
|
// send data to server
|
||||||
// webSocket.sendBIN(payload, lenght);
|
// webSocket.sendBIN(payload, length);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,10 +73,18 @@ void setup() {
|
|||||||
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, "/");
|
||||||
|
|
||||||
|
// event handler
|
||||||
webSocket.onEvent(webSocketEvent);
|
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() {
|
||||||
|
0
examples/WebSocketClientAVR/.esp8266.skip
Normal file
0
examples/WebSocketClientAVR/.esp8266.skip
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
/*
|
||||||
|
WebSocketClientSockJsAndStomp.ino
|
||||||
|
|
||||||
|
Example for connecting and maintining a connection with a SockJS+STOMP websocket connection.
|
||||||
|
In this example we connect to a Spring application (see https://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html).
|
||||||
|
|
||||||
|
Created on: 18.07.2017
|
||||||
|
Author: Martin Becker <mgbckr>, Contact: becker@informatik.uni-wuerzburg.de
|
||||||
|
*/
|
||||||
|
|
||||||
|
// PRE
|
||||||
|
|
||||||
|
#define USE_SERIAL Serial
|
||||||
|
|
||||||
|
|
||||||
|
// LIBRARIES
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <Hash.h>
|
||||||
|
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <WebSocketsClient.h>
|
||||||
|
|
||||||
|
|
||||||
|
// SETTINGS
|
||||||
|
|
||||||
|
const char* wlan_ssid = "yourssid";
|
||||||
|
const char* wlan_password = "somepassword";
|
||||||
|
|
||||||
|
const char* ws_host = "the.host.net";
|
||||||
|
const int ws_port = 80;
|
||||||
|
|
||||||
|
// base URL for SockJS (websocket) connection
|
||||||
|
// The complete URL will look something like this(cf. http://sockjs.github.io/sockjs-protocol/sockjs-protocol-0.3.3.html#section-36):
|
||||||
|
// ws://<ws_host>:<ws_port>/<ws_baseurl>/<3digits>/<randomstring>/websocket
|
||||||
|
// For the default config of Spring's SockJS/STOMP support the default base URL is "/socketentry/".
|
||||||
|
const char* ws_baseurl = "/socketentry/"; // don't forget leading and trailing "/" !!!
|
||||||
|
|
||||||
|
|
||||||
|
// VARIABLES
|
||||||
|
|
||||||
|
WebSocketsClient webSocket;
|
||||||
|
|
||||||
|
|
||||||
|
// FUNCTIONS
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case WStype_TEXT:
|
||||||
|
{
|
||||||
|
// #####################
|
||||||
|
// handle STOMP protocol
|
||||||
|
// #####################
|
||||||
|
|
||||||
|
String text = (char*) payload;
|
||||||
|
|
||||||
|
USE_SERIAL.printf("[WSc] get text: %s\n", payload);
|
||||||
|
|
||||||
|
if (payload[0] == 'h') {
|
||||||
|
|
||||||
|
USE_SERIAL.println("Heartbeat!");
|
||||||
|
|
||||||
|
} else if (payload[0] == 'o') {
|
||||||
|
|
||||||
|
// on open connection
|
||||||
|
char *msg = "[\"CONNECT\\naccept-version:1.1,1.0\\nheart-beat:10000,10000\\n\\n\\u0000\"]";
|
||||||
|
webSocket.sendTXT(msg);
|
||||||
|
|
||||||
|
} else if (text.startsWith("a[\"CONNECTED")) {
|
||||||
|
|
||||||
|
// subscribe to some channels
|
||||||
|
|
||||||
|
char *msg = "[\"SUBSCRIBE\\nid:sub-0\\ndestination:/user/queue/messages\\n\\n\\u0000\"]";
|
||||||
|
webSocket.sendTXT(msg);
|
||||||
|
delay(1000);
|
||||||
|
|
||||||
|
// and send a message
|
||||||
|
|
||||||
|
msg = "[\"SEND\\ndestination:/app/message\\n\\n{\\\"user\\\":\\\"esp\\\",\\\"message\\\":\\\"Hello!\\\"}\\u0000\"]";
|
||||||
|
webSocket.sendTXT(msg);
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
|
||||||
|
// setup serial
|
||||||
|
|
||||||
|
// USE_SERIAL.begin(921600);
|
||||||
|
USE_SERIAL.begin(115200);
|
||||||
|
|
||||||
|
// USE_SERIAL.setDebugOutput(true);
|
||||||
|
|
||||||
|
USE_SERIAL.println();
|
||||||
|
|
||||||
|
|
||||||
|
// connect to WiFi
|
||||||
|
|
||||||
|
USE_SERIAL.print("Logging into WLAN: "); Serial.print(wlan_ssid); Serial.print(" ...");
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFi.begin(wlan_ssid, wlan_password);
|
||||||
|
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
USE_SERIAL.print(".");
|
||||||
|
}
|
||||||
|
USE_SERIAL.println(" success.");
|
||||||
|
USE_SERIAL.print("IP: "); USE_SERIAL.println(WiFi.localIP());
|
||||||
|
|
||||||
|
|
||||||
|
// #####################
|
||||||
|
// create socket url according to SockJS protocol (cf. http://sockjs.github.io/sockjs-protocol/sockjs-protocol-0.3.3.html#section-36)
|
||||||
|
// #####################
|
||||||
|
String socketUrl = ws_baseurl;
|
||||||
|
socketUrl += random(0, 999);
|
||||||
|
socketUrl += "/";
|
||||||
|
socketUrl += random(0, 999999); // should be a random string, but this works (see )
|
||||||
|
socketUrl += "/websocket";
|
||||||
|
|
||||||
|
// connect to websocket
|
||||||
|
webSocket.begin(ws_host, ws_port, socketUrl);
|
||||||
|
webSocket.setExtraHeaders(); // remove "Origin: file://" header because it breaks the connection with Spring's default websocket config
|
||||||
|
// webSocket.setExtraHeaders("foo: I am so funny\r\nbar: not"); // some headers, in case you feel funny
|
||||||
|
webSocket.onEvent(webSocketEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
webSocket.loop();
|
||||||
|
}
|
@ -27,7 +27,7 @@ uint64_t messageTimestamp = 0;
|
|||||||
uint64_t heartbeatTimestamp = 0;
|
uint64_t heartbeatTimestamp = 0;
|
||||||
bool isConnected = false;
|
bool isConnected = false;
|
||||||
|
|
||||||
void webSocketEvent(WStype_t type, uint8_t * payload, size_t lenght) {
|
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
|
||||||
|
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
@ -52,11 +52,11 @@ void webSocketEvent(WStype_t type, uint8_t * payload, size_t lenght) {
|
|||||||
// webSocket.sendTXT("message here");
|
// webSocket.sendTXT("message here");
|
||||||
break;
|
break;
|
||||||
case WStype_BIN:
|
case WStype_BIN:
|
||||||
USE_SERIAL.printf("[WSc] get binary lenght: %u\n", lenght);
|
USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
|
||||||
hexdump(payload, lenght);
|
hexdump(payload, length);
|
||||||
|
|
||||||
// send data to server
|
// send data to server
|
||||||
// webSocket.sendBIN(payload, lenght);
|
// webSocket.sendBIN(payload, length);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ WebSocketsServer webSocket = WebSocketsServer(81);
|
|||||||
|
|
||||||
#define USE_SERIAL Serial1
|
#define USE_SERIAL Serial1
|
||||||
|
|
||||||
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case WStype_DISCONNECTED:
|
case WStype_DISCONNECTED:
|
||||||
@ -43,11 +43,11 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
|
|||||||
// webSocket.broadcastTXT("message here");
|
// webSocket.broadcastTXT("message here");
|
||||||
break;
|
break;
|
||||||
case WStype_BIN:
|
case WStype_BIN:
|
||||||
USE_SERIAL.printf("[%u] get binary lenght: %u\n", num, lenght);
|
USE_SERIAL.printf("[%u] get binary length: %u\n", num, length);
|
||||||
hexdump(payload, lenght);
|
hexdump(payload, length);
|
||||||
|
|
||||||
// send message to client
|
// send message to client
|
||||||
// webSocket.sendBIN(num, payload, lenght);
|
// webSocket.sendBIN(num, payload, length);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
* WebSocketServer.ino
|
||||||
|
*
|
||||||
|
* Created on: 22.05.2015
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ESP8266WiFiMulti.h>
|
||||||
|
#include <WebSocketsServer.h>
|
||||||
|
#include <Hash.h>
|
||||||
|
|
||||||
|
ESP8266WiFiMulti WiFiMulti;
|
||||||
|
|
||||||
|
WebSocketsServer webSocket = WebSocketsServer(81);
|
||||||
|
|
||||||
|
#define USE_SERIAL Serial
|
||||||
|
|
||||||
|
String fragmentBuffer = "";
|
||||||
|
|
||||||
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
|
||||||
|
|
||||||
|
switch(type) {
|
||||||
|
case WStype_DISCONNECTED:
|
||||||
|
USE_SERIAL.printf("[%u] Disconnected!\n", num);
|
||||||
|
break;
|
||||||
|
case WStype_CONNECTED: {
|
||||||
|
IPAddress ip = webSocket.remoteIP(num);
|
||||||
|
USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
|
||||||
|
|
||||||
|
// send message to client
|
||||||
|
webSocket.sendTXT(num, "Connected");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case WStype_TEXT:
|
||||||
|
USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);
|
||||||
|
break;
|
||||||
|
case WStype_BIN:
|
||||||
|
USE_SERIAL.printf("[%u] get binary length: %u\n", num, length);
|
||||||
|
hexdump(payload, length);
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Fragmentation / continuation opcode handling
|
||||||
|
// case WStype_FRAGMENT_BIN_START:
|
||||||
|
case WStype_FRAGMENT_TEXT_START:
|
||||||
|
fragmentBuffer = (char*)payload;
|
||||||
|
USE_SERIAL.printf("[%u] get start start of Textfragment: %s\n", num, payload);
|
||||||
|
break;
|
||||||
|
case WStype_FRAGMENT:
|
||||||
|
fragmentBuffer += (char*)payload;
|
||||||
|
USE_SERIAL.printf("[%u] get Textfragment : %s\n", num, payload);
|
||||||
|
break;
|
||||||
|
case WStype_FRAGMENT_FIN:
|
||||||
|
fragmentBuffer += (char*)payload;
|
||||||
|
USE_SERIAL.printf("[%u] get end of Textfragment: %s\n", num, payload);
|
||||||
|
USE_SERIAL.printf("[%u] full frame: %s\n", num, fragmentBuffer.c_str());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
// USE_SERIAL.begin(921600);
|
||||||
|
USE_SERIAL.begin(115200);
|
||||||
|
|
||||||
|
//Serial.setDebugOutput(true);
|
||||||
|
USE_SERIAL.setDebugOutput(true);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
WiFiMulti.addAP("SSID", "passpasspass");
|
||||||
|
|
||||||
|
while(WiFiMulti.run() != WL_CONNECTED) {
|
||||||
|
delay(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
webSocket.begin();
|
||||||
|
webSocket.onEvent(webSocketEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
webSocket.loop();
|
||||||
|
}
|
||||||
|
|
@ -26,7 +26,7 @@ ESP8266WiFiMulti WiFiMulti;
|
|||||||
ESP8266WebServer server = ESP8266WebServer(80);
|
ESP8266WebServer server = ESP8266WebServer(80);
|
||||||
WebSocketsServer webSocket = WebSocketsServer(81);
|
WebSocketsServer webSocket = WebSocketsServer(81);
|
||||||
|
|
||||||
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case WStype_DISCONNECTED:
|
case WStype_DISCONNECTED:
|
||||||
@ -100,7 +100,7 @@ void setup() {
|
|||||||
// handle index
|
// handle index
|
||||||
server.on("/", []() {
|
server.on("/", []() {
|
||||||
// send index.html
|
// send index.html
|
||||||
server.send(200, "text/html", "<html><head><script>var connection = new WebSocket('ws://'+location.hostname+':81/', ['arduino']);connection.onopen = function () { connection.send('Connect ' + new Date()); }; connection.onerror = function (error) { console.log('WebSocket Error ', error);};connection.onmessage = function (e) { console.log('Server: ', e.data);};function sendRGB() { var r = parseInt(document.getElementById('r').value).toString(16); var g = parseInt(document.getElementById('g').value).toString(16); var b = parseInt(document.getElementById('b').value).toString(16); if(r.length < 2) { r = '0' + r; } if(g.length < 2) { g = '0' + g; } if(b.length < 2) { b = '0' + b; } var rgb = '#'+r+g+b; console.log('RGB: ' + rgb); connection.send(rgb); }</script></head><body>LED Control:<br/><br/>R: <input id=\"r\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" onchange=\"sendRGB();\" /><br/>G: <input id=\"g\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" onchange=\"sendRGB();\" /><br/>B: <input id=\"b\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" onchange=\"sendRGB();\" /><br/></body></html>");
|
server.send(200, "text/html", "<html><head><script>var connection = new WebSocket('ws://'+location.hostname+':81/', ['arduino']);connection.onopen = function () { connection.send('Connect ' + new Date()); }; connection.onerror = function (error) { console.log('WebSocket Error ', error);};connection.onmessage = function (e) { console.log('Server: ', e.data);};function sendRGB() { var r = parseInt(document.getElementById('r').value).toString(16); var g = parseInt(document.getElementById('g').value).toString(16); var b = parseInt(document.getElementById('b').value).toString(16); if(r.length < 2) { r = '0' + r; } if(g.length < 2) { g = '0' + g; } if(b.length < 2) { b = '0' + b; } var rgb = '#'+r+g+b; console.log('RGB: ' + rgb); connection.send(rgb); }</script></head><body>LED Control:<br/><br/>R: <input id=\"r\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>G: <input id=\"g\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>B: <input id=\"b\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/></body></html>");
|
||||||
});
|
});
|
||||||
|
|
||||||
server.begin();
|
server.begin();
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/Links2004/arduinoWebSockets.git"
|
"url": "https://github.com/Links2004/arduinoWebSockets.git"
|
||||||
},
|
},
|
||||||
"version": "2.0.6",
|
"version": "2.0.9",
|
||||||
"license": "LGPL-2.1",
|
"license": "LGPL-2.1",
|
||||||
"export": {
|
"export": {
|
||||||
"exclude": [
|
"exclude": [
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name=WebSockets
|
name=WebSockets
|
||||||
version=2.0.6
|
version=2.0.9
|
||||||
author=Markus Sattler
|
author=Markus Sattler
|
||||||
maintainer=Markus Sattler
|
maintainer=Markus Sattler
|
||||||
sentence=WebSockets for Arduino (Server + Client)
|
sentence=WebSockets for Arduino (Server + Client)
|
||||||
|
@ -233,18 +233,18 @@ bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
|
|||||||
// header has be added to payload
|
// header has be added to payload
|
||||||
// payload is forced to reserved 14 Byte but we may not need all based on the length and mask settings
|
// payload is forced to reserved 14 Byte but we may not need all based on the length and mask settings
|
||||||
// offset in payload is calculatetd 14 - headerSize
|
// offset in payload is calculatetd 14 - headerSize
|
||||||
if(client->tcp->write(&payloadPtr[(WEBSOCKETS_MAX_HEADER_SIZE - headerSize)], (length + headerSize)) != (length + headerSize)) {
|
if(write(client, &payloadPtr[(WEBSOCKETS_MAX_HEADER_SIZE - headerSize)], (length + headerSize)) != (length + headerSize)) {
|
||||||
ret = false;
|
ret = false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// send header
|
// send header
|
||||||
if(client->tcp->write(&buffer[0], headerSize) != headerSize) {
|
if(write(client, &buffer[0], headerSize) != headerSize) {
|
||||||
ret = false;
|
ret = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(payloadPtr && length > 0) {
|
if(payloadPtr && length > 0) {
|
||||||
// send payload
|
// send payload
|
||||||
if(client->tcp->write(&payloadPtr[0], length) != length) {
|
if(write(client, &payloadPtr[0], length) != length) {
|
||||||
ret = false;
|
ret = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -426,14 +426,15 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
|
|||||||
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] text: %s\n", client->num, payload);
|
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] text: %s\n", client->num, payload);
|
||||||
// no break here!
|
// no break here!
|
||||||
case WSop_binary:
|
case WSop_binary:
|
||||||
messageReceived(client, header->opCode, payload, header->payloadLen);
|
case WSop_continuation:
|
||||||
|
messageReceived(client, header->opCode, payload, header->payloadLen, header->fin);
|
||||||
break;
|
break;
|
||||||
case WSop_ping:
|
case WSop_ping:
|
||||||
// send pong back
|
// send pong back
|
||||||
sendFrame(client, WSop_pong, payload, header->payloadLen, true);
|
sendFrame(client, WSop_pong, payload, header->payloadLen, true);
|
||||||
break;
|
break;
|
||||||
case WSop_pong:
|
case WSop_pong:
|
||||||
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get pong (%s)\n", client->num, payload);
|
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get pong (%s)\n", client->num, payload ? (const char*)payload : "");
|
||||||
break;
|
break;
|
||||||
case WSop_close: {
|
case WSop_close: {
|
||||||
uint16_t reasonCode = 1000;
|
uint16_t reasonCode = 1000;
|
||||||
@ -450,10 +451,6 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
|
|||||||
clientDisconnect(client, 1000);
|
clientDisconnect(client, 1000);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case WSop_continuation:
|
|
||||||
// continuation is not supported
|
|
||||||
clientDisconnect(client, 1003);
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
clientDisconnect(client, 1002);
|
clientDisconnect(client, 1002);
|
||||||
break;
|
break;
|
||||||
@ -596,3 +593,56 @@ bool WebSockets::readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWait
|
|||||||
#endif
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* write x byte to tcp or get timeout
|
||||||
|
* @param client WSclient_t *
|
||||||
|
* @param out uint8_t * data buffer
|
||||||
|
* @param n size_t byte count
|
||||||
|
* @return bytes send
|
||||||
|
*/
|
||||||
|
size_t WebSockets::write(WSclient_t * client, uint8_t *out, size_t n) {
|
||||||
|
if(out == NULL) return 0;
|
||||||
|
if(client == NULL) return 0;
|
||||||
|
unsigned long t = millis();
|
||||||
|
size_t len = 0;
|
||||||
|
size_t total = 0;
|
||||||
|
DEBUG_WEBSOCKETS("[write] n: %d t: %d\n", n, t);
|
||||||
|
while(n > 0) {
|
||||||
|
if(client->tcp == NULL) {
|
||||||
|
DEBUG_WEBSOCKETS("[write] tcp is null!\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!client->tcp->connected()) {
|
||||||
|
DEBUG_WEBSOCKETS("[write] not connected!\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if((millis() - t) > WEBSOCKETS_TCP_TIMEOUT) {
|
||||||
|
DEBUG_WEBSOCKETS("[write] write TIMEOUT! %d\n", (millis() - t));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
len = client->tcp->write((const uint8_t*)out, n);
|
||||||
|
if(len) {
|
||||||
|
t = millis();
|
||||||
|
out += len;
|
||||||
|
n -= len;
|
||||||
|
total += len;
|
||||||
|
//DEBUG_WEBSOCKETS("write %d left %d!\n", len, n);
|
||||||
|
} else {
|
||||||
|
//DEBUG_WEBSOCKETS("write %d failed left %d!\n", len, n);
|
||||||
|
}
|
||||||
|
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
|
||||||
|
delay(0);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t WebSockets::write(WSclient_t * client, const char *out) {
|
||||||
|
if(client == NULL) return 0;
|
||||||
|
if(out == NULL) return 0;
|
||||||
|
return write(client, (uint8_t*)out, strlen(out));
|
||||||
|
}
|
||||||
|
@ -32,7 +32,16 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
#ifndef NODEBUG_WEBSOCKETS
|
||||||
|
#ifdef DEBUG_ESP_PORT
|
||||||
|
#define DEBUG_WEBSOCKETS(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ )
|
||||||
|
#else
|
||||||
//#define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ )
|
//#define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ )
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifndef DEBUG_WEBSOCKETS
|
#ifndef DEBUG_WEBSOCKETS
|
||||||
#define DEBUG_WEBSOCKETS(...)
|
#define DEBUG_WEBSOCKETS(...)
|
||||||
@ -43,6 +52,8 @@
|
|||||||
#define WEBSOCKETS_MAX_DATA_SIZE (15*1024)
|
#define WEBSOCKETS_MAX_DATA_SIZE (15*1024)
|
||||||
#define WEBSOCKETS_USE_BIG_MEM
|
#define WEBSOCKETS_USE_BIG_MEM
|
||||||
#define GET_FREE_HEAP ESP.getFreeHeap()
|
#define GET_FREE_HEAP ESP.getFreeHeap()
|
||||||
|
// moves all Header strings to Flash (~300 Byte)
|
||||||
|
//#define WEBSOCKETS_SAVE_RAM
|
||||||
#else
|
#else
|
||||||
#ifdef STM32_DEVICE
|
#ifdef STM32_DEVICE
|
||||||
#define WEBSOCKETS_MAX_DATA_SIZE (15*1024)
|
#define WEBSOCKETS_MAX_DATA_SIZE (15*1024)
|
||||||
@ -51,6 +62,8 @@
|
|||||||
#else
|
#else
|
||||||
//atmega328p has only 2KB ram!
|
//atmega328p has only 2KB ram!
|
||||||
#define WEBSOCKETS_MAX_DATA_SIZE (1024)
|
#define WEBSOCKETS_MAX_DATA_SIZE (1024)
|
||||||
|
// moves all Header strings to Flash
|
||||||
|
#define WEBSOCKETS_SAVE_RAM
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -69,6 +82,7 @@
|
|||||||
#if defined(ESP8266) || defined(ESP31B)
|
#if defined(ESP8266) || defined(ESP31B)
|
||||||
#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266
|
#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266
|
||||||
//#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266_ASYNC
|
//#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266_ASYNC
|
||||||
|
//#define WEBSOCKETS_NETWORK_TYPE NETWORK_W5100
|
||||||
#else
|
#else
|
||||||
#define WEBSOCKETS_NETWORK_TYPE NETWORK_W5100
|
#define WEBSOCKETS_NETWORK_TYPE NETWORK_W5100
|
||||||
#endif
|
#endif
|
||||||
@ -130,6 +144,12 @@
|
|||||||
#error "no network type selected!"
|
#error "no network type selected!"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// moves all Header strings to Flash (~300 Byte)
|
||||||
|
#ifdef WEBSOCKETS_SAVE_RAM
|
||||||
|
#define WEBSOCKETS_STRING(var) F(var)
|
||||||
|
#else
|
||||||
|
#define WEBSOCKETS_STRING(var) var
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
WSC_NOT_CONNECTED,
|
WSC_NOT_CONNECTED,
|
||||||
@ -142,7 +162,11 @@ typedef enum {
|
|||||||
WStype_DISCONNECTED,
|
WStype_DISCONNECTED,
|
||||||
WStype_CONNECTED,
|
WStype_CONNECTED,
|
||||||
WStype_TEXT,
|
WStype_TEXT,
|
||||||
WStype_BIN
|
WStype_BIN,
|
||||||
|
WStype_FRAGMENT_TEXT_START,
|
||||||
|
WStype_FRAGMENT_BIN_START,
|
||||||
|
WStype_FRAGMENT,
|
||||||
|
WStype_FRAGMENT_FIN,
|
||||||
} WStype_t;
|
} WStype_t;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
@ -205,6 +229,8 @@ typedef struct {
|
|||||||
String base64Authorization; ///< Base64 encoded Auth request
|
String base64Authorization; ///< Base64 encoded Auth request
|
||||||
String plainAuthorization; ///< Base64 encoded Auth request
|
String plainAuthorization; ///< Base64 encoded Auth request
|
||||||
|
|
||||||
|
String extraHeaders;
|
||||||
|
|
||||||
bool cHttpHeadersValid; ///< non-websocket http header validity indicator
|
bool cHttpHeadersValid; ///< non-websocket http header validity indicator
|
||||||
size_t cMandatoryHeadersCount; ///< non-websocket mandatory http headers present count
|
size_t cMandatoryHeadersCount; ///< non-websocket mandatory http headers present count
|
||||||
|
|
||||||
@ -227,7 +253,7 @@ class WebSockets {
|
|||||||
virtual void clientDisconnect(WSclient_t * client);
|
virtual void clientDisconnect(WSclient_t * client);
|
||||||
virtual bool clientIsConnected(WSclient_t * client);
|
virtual bool clientIsConnected(WSclient_t * client);
|
||||||
|
|
||||||
virtual void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length);
|
virtual void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin);
|
||||||
|
|
||||||
void clientDisconnect(WSclient_t * client, uint16_t code, char * reason = NULL, size_t reasonLen = 0);
|
void clientDisconnect(WSclient_t * client, uint16_t code, char * reason = NULL, size_t reasonLen = 0);
|
||||||
bool sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload = NULL, size_t length = 0, bool mask = false, bool fin = true, bool headerToPayload = false);
|
bool sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * payload = NULL, size_t length = 0, bool mask = false, bool fin = true, bool headerToPayload = false);
|
||||||
@ -244,6 +270,8 @@ class WebSockets {
|
|||||||
String base64_encode(uint8_t * data, size_t length);
|
String base64_encode(uint8_t * data, size_t length);
|
||||||
|
|
||||||
bool readCb(WSclient_t * client, uint8_t *out, size_t n, WSreadWaitCb cb);
|
bool readCb(WSclient_t * client, uint8_t *out, size_t n, WSreadWaitCb cb);
|
||||||
|
virtual size_t write(WSclient_t * client, uint8_t *out, size_t n);
|
||||||
|
size_t write(WSclient_t * client, const char *out);
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
WebSocketsClient::WebSocketsClient() {
|
WebSocketsClient::WebSocketsClient() {
|
||||||
_cbEvent = NULL;
|
_cbEvent = NULL;
|
||||||
_client.num = 0;
|
_client.num = 0;
|
||||||
|
_client.extraHeaders = WEBSOCKETS_STRING("Origin: file://");
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocketsClient::~WebSocketsClient() {
|
WebSocketsClient::~WebSocketsClient() {
|
||||||
@ -74,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) {
|
||||||
@ -101,12 +105,29 @@ void WebSocketsClient::beginSocketIO(String host, uint16_t port, String url, Str
|
|||||||
beginSocketIO(host.c_str(), port, url.c_str(), protocol.c_str());
|
beginSocketIO(host.c_str(), port, url.c_str(), protocol.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
|
||||||
|
void WebSocketsClient::beginSocketIOSSL(const char *host, uint16_t port, const char * url, const char * protocol) {
|
||||||
|
begin(host, port, url, protocol);
|
||||||
|
_client.isSocketIO = true;
|
||||||
|
_client.isSSL = true;
|
||||||
|
_fingerprint = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebSocketsClient::beginSocketIOSSL(String host, uint16_t port, String url, String protocol) {
|
||||||
|
beginSocketIOSSL(host.c_str(), port, url.c_str(), protocol.c_str());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
|
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
|
||||||
/**
|
/**
|
||||||
* called in arduino loop
|
* called in arduino loop
|
||||||
*/
|
*/
|
||||||
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) {
|
||||||
@ -137,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();
|
||||||
@ -261,6 +284,25 @@ void WebSocketsClient::setAuthorization(const char * 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) {
|
||||||
|
_reconnectInterval = time;
|
||||||
|
}
|
||||||
|
|
||||||
//#################################################################################
|
//#################################################################################
|
||||||
//#################################################################################
|
//#################################################################################
|
||||||
//#################################################################################
|
//#################################################################################
|
||||||
@ -270,21 +312,24 @@ void WebSocketsClient::setAuthorization(const char * auth) {
|
|||||||
* @param client WSclient_t * ptr to the client struct
|
* @param client WSclient_t * ptr to the client struct
|
||||||
* @param opcode WSopcode_t
|
* @param opcode WSopcode_t
|
||||||
* @param payload uint8_t *
|
* @param payload uint8_t *
|
||||||
* @param lenght size_t
|
* @param length size_t
|
||||||
*/
|
*/
|
||||||
void WebSocketsClient::messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t lenght) {
|
void WebSocketsClient::messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin) {
|
||||||
WStype_t type = WStype_ERROR;
|
WStype_t type = WStype_ERROR;
|
||||||
|
|
||||||
switch(opcode) {
|
switch(opcode) {
|
||||||
case WSop_text:
|
case WSop_text:
|
||||||
type = WStype_TEXT;
|
type = fin ? WStype_TEXT : WStype_FRAGMENT_TEXT_START;
|
||||||
break;
|
break;
|
||||||
case WSop_binary:
|
case WSop_binary:
|
||||||
type = WStype_BIN;
|
type = fin ? WStype_BIN : WStype_FRAGMENT_BIN_START;
|
||||||
|
break;
|
||||||
|
case WSop_continuation:
|
||||||
|
type = fin ? WStype_FRAGMENT_FIN : WStype_FRAGMENT;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
runCbEvent(type, payload, lenght);
|
runCbEvent(type, payload, length);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -401,12 +446,15 @@ void WebSocketsClient::handleClientData(void) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* send the WebSocket header to Server
|
* send the WebSocket header to Server
|
||||||
* @param client WSclient_t * ptr to the client struct
|
* @param client WSclient_t * ptr to the client struct
|
||||||
*/
|
*/
|
||||||
void WebSocketsClient::sendHeader(WSclient_t * client) {
|
void WebSocketsClient::sendHeader(WSclient_t * client) {
|
||||||
|
|
||||||
|
static const char * NEW_LINE = "\r\n";
|
||||||
|
|
||||||
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header...\n");
|
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header...\n");
|
||||||
|
|
||||||
uint8_t randomKey[16] = { 0 };
|
uint8_t randomKey[16] = { 0 };
|
||||||
@ -421,48 +469,66 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
|
|||||||
unsigned long start = micros();
|
unsigned long start = micros();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
String transport;
|
|
||||||
String handshake;
|
String handshake;
|
||||||
if(!client->isSocketIO || (client->isSocketIO && client->cSessionId.length() > 0)) {
|
bool ws_header = true;
|
||||||
|
String url = client->cUrl;
|
||||||
|
|
||||||
if(client->isSocketIO) {
|
if(client->isSocketIO) {
|
||||||
transport = "&transport=websocket&sid=" + client->cSessionId;
|
if(client->cSessionId.length() == 0) {
|
||||||
|
url += WEBSOCKETS_STRING("&transport=polling");
|
||||||
|
ws_header = false;
|
||||||
|
} else {
|
||||||
|
url += WEBSOCKETS_STRING("&transport=websocket&sid=");
|
||||||
|
url += client->cSessionId;
|
||||||
}
|
}
|
||||||
handshake = "GET " + client->cUrl + transport + " HTTP/1.1\r\n"
|
}
|
||||||
"Host: " + _host + ":" + _port + "\r\n"
|
|
||||||
"Connection: Upgrade\r\n"
|
handshake = WEBSOCKETS_STRING("GET ");
|
||||||
|
handshake += url + WEBSOCKETS_STRING(" HTTP/1.1\r\n"
|
||||||
|
"Host: ");
|
||||||
|
handshake += _host + ":" + _port + NEW_LINE;
|
||||||
|
|
||||||
|
if(ws_header) {
|
||||||
|
handshake += WEBSOCKETS_STRING("Connection: Upgrade\r\n"
|
||||||
"Upgrade: websocket\r\n"
|
"Upgrade: websocket\r\n"
|
||||||
"Sec-WebSocket-Version: 13\r\n"
|
"Sec-WebSocket-Version: 13\r\n"
|
||||||
"Sec-WebSocket-Key: " + client->cKey + "\r\n";
|
"Sec-WebSocket-Key: ");
|
||||||
|
handshake += client->cKey + NEW_LINE;
|
||||||
|
|
||||||
if(client->cProtocol.length() > 0) {
|
if(client->cProtocol.length() > 0) {
|
||||||
handshake += "Sec-WebSocket-Protocol: " + client->cProtocol + "\r\n";
|
handshake += WEBSOCKETS_STRING("Sec-WebSocket-Protocol: ");
|
||||||
|
handshake +=client->cProtocol + NEW_LINE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(client->cExtensions.length() > 0) {
|
if(client->cExtensions.length() > 0) {
|
||||||
handshake += "Sec-WebSocket-Extensions: " + client->cExtensions + "\r\n";
|
handshake += WEBSOCKETS_STRING("Sec-WebSocket-Extensions: ");
|
||||||
|
handshake +=client->cExtensions + NEW_LINE;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
handshake = "GET " + client->cUrl + "&transport=polling HTTP/1.1\r\n"
|
handshake += WEBSOCKETS_STRING("Connection: keep-alive\r\n");
|
||||||
"Host: " + _host + ":" + _port + "\r\n"
|
|
||||||
"Connection: keep-alive\r\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
handshake += "Origin: file://\r\n"
|
// add extra headers; by default this includes "Origin: file://"
|
||||||
"User-Agent: arduino-WebSocket-Client\r\n";
|
if (client->extraHeaders) {
|
||||||
|
handshake += client->extraHeaders + NEW_LINE;
|
||||||
|
}
|
||||||
|
|
||||||
|
handshake += WEBSOCKETS_STRING("User-Agent: arduino-WebSocket-Client\r\n");
|
||||||
|
|
||||||
if(client->base64Authorization.length() > 0) {
|
if(client->base64Authorization.length() > 0) {
|
||||||
handshake += "Authorization: Basic " + client->base64Authorization + "\r\n";
|
handshake += WEBSOCKETS_STRING("Authorization: Basic ");
|
||||||
|
handshake += client->base64Authorization + NEW_LINE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(client->plainAuthorization.length() > 0) {
|
if(client->plainAuthorization.length() > 0) {
|
||||||
handshake += "Authorization: " + client->plainAuthorization + "\r\n";
|
handshake += WEBSOCKETS_STRING("Authorization: ");
|
||||||
|
handshake += client->plainAuthorization + NEW_LINE;
|
||||||
}
|
}
|
||||||
|
|
||||||
handshake += "\r\n";
|
handshake += NEW_LINE;
|
||||||
|
|
||||||
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] handshake %s", (uint8_t*)handshake.c_str());
|
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] handshake %s", (uint8_t*)handshake.c_str());
|
||||||
client->tcp->write((uint8_t*)handshake.c_str(), handshake.length());
|
write(client, (uint8_t*)handshake.c_str(), handshake.length());
|
||||||
|
|
||||||
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
||||||
client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsClient::handleHeader, this, client, &(client->cHttpLine)));
|
client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsClient::handleHeader, this, client, &(client->cHttpLine)));
|
||||||
@ -483,32 +549,32 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
|
|||||||
if(headerLine->length() > 0) {
|
if(headerLine->length() > 0) {
|
||||||
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] RX: %s\n", headerLine->c_str());
|
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] RX: %s\n", headerLine->c_str());
|
||||||
|
|
||||||
if(headerLine->startsWith("HTTP/1.")) {
|
if(headerLine->startsWith(WEBSOCKETS_STRING("HTTP/1."))) {
|
||||||
// "HTTP/1.1 101 Switching Protocols"
|
// "HTTP/1.1 101 Switching Protocols"
|
||||||
client->cCode = headerLine->substring(9, headerLine->indexOf(' ', 9)).toInt();
|
client->cCode = headerLine->substring(9, headerLine->indexOf(' ', 9)).toInt();
|
||||||
} else if(headerLine->indexOf(':')) {
|
} else if(headerLine->indexOf(':')) {
|
||||||
String headerName = headerLine->substring(0, headerLine->indexOf(':'));
|
String headerName = headerLine->substring(0, headerLine->indexOf(':'));
|
||||||
String headerValue = headerLine->substring(headerLine->indexOf(':') + 2);
|
String headerValue = headerLine->substring(headerLine->indexOf(':') + 2);
|
||||||
|
|
||||||
if(headerName.equalsIgnoreCase("Connection")) {
|
if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Connection"))) {
|
||||||
if(headerValue.equalsIgnoreCase("upgrade")) {
|
if(headerValue.equalsIgnoreCase(WEBSOCKETS_STRING("upgrade"))) {
|
||||||
client->cIsUpgrade = true;
|
client->cIsUpgrade = true;
|
||||||
}
|
}
|
||||||
} else if(headerName.equalsIgnoreCase("Upgrade")) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Upgrade"))) {
|
||||||
if(headerValue.equalsIgnoreCase("websocket")) {
|
if(headerValue.equalsIgnoreCase(WEBSOCKETS_STRING("websocket"))) {
|
||||||
client->cIsWebsocket = true;
|
client->cIsWebsocket = true;
|
||||||
}
|
}
|
||||||
} else if(headerName.equalsIgnoreCase("Sec-WebSocket-Accept")) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Accept"))) {
|
||||||
client->cAccept = headerValue;
|
client->cAccept = headerValue;
|
||||||
client->cAccept.trim(); // see rfc6455
|
client->cAccept.trim(); // see rfc6455
|
||||||
} else if(headerName.equalsIgnoreCase("Sec-WebSocket-Protocol")) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Protocol"))) {
|
||||||
client->cProtocol = headerValue;
|
client->cProtocol = headerValue;
|
||||||
} else if(headerName.equalsIgnoreCase("Sec-WebSocket-Extensions")) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Extensions"))) {
|
||||||
client->cExtensions = headerValue;
|
client->cExtensions = headerValue;
|
||||||
} else if(headerName.equalsIgnoreCase("Sec-WebSocket-Version")) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Version"))) {
|
||||||
client->cVersion = headerValue.toInt();
|
client->cVersion = headerValue.toInt();
|
||||||
} else if(headerName.equalsIgnoreCase("Set-Cookie")) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Set-Cookie"))) {
|
||||||
if (headerValue.indexOf("HttpOnly") > -1) {
|
if (headerValue.indexOf(WEBSOCKETS_STRING("HttpOnly")) > -1) {
|
||||||
client->cSessionId = headerValue.substring(headerValue.indexOf('=') + 1, headerValue.indexOf(";"));
|
client->cSessionId = headerValue.substring(headerValue.indexOf('=') + 1, headerValue.indexOf(";"));
|
||||||
} else {
|
} else {
|
||||||
client->cSessionId = headerValue.substring(headerValue.indexOf('=') + 1);
|
client->cSessionId = headerValue.substring(headerValue.indexOf('=') + 1);
|
||||||
@ -557,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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -580,14 +647,16 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
|
|||||||
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Websocket connection init done.\n");
|
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Websocket connection init done.\n");
|
||||||
headerDone(client);
|
headerDone(client);
|
||||||
|
|
||||||
|
|
||||||
runCbEvent(WStype_CONNECTED, (uint8_t *) client->cUrl.c_str(), client->cUrl.length());
|
runCbEvent(WStype_CONNECTED, (uint8_t *) client->cUrl.c_str(), client->cUrl.length());
|
||||||
|
|
||||||
} else if(clientIsConnected(client) && client->isSocketIO && client->cSessionId.length() > 0) {
|
} else if(clientIsConnected(client) && client->isSocketIO && client->cSessionId.length() > 0) {
|
||||||
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");
|
||||||
client->tcp->write("This is a webSocket client!");
|
_lastConnectionFail = millis();
|
||||||
|
if(clientIsConnected(client)) {
|
||||||
|
write(client, "This is a webSocket client!");
|
||||||
|
}
|
||||||
clientDisconnect(client);
|
clientDisconnect(client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,11 @@ class WebSocketsClient: private WebSockets {
|
|||||||
void beginSocketIO(const char *host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
|
void beginSocketIO(const char *host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
|
||||||
void beginSocketIO(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
|
void beginSocketIO(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
|
||||||
|
|
||||||
|
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
|
||||||
|
void beginSocketIOSSL(const char *host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
|
||||||
|
void beginSocketIOSSL(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
|
||||||
|
#endif
|
||||||
|
|
||||||
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
|
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
|
||||||
void loop(void);
|
void loop(void);
|
||||||
#else
|
#else
|
||||||
@ -76,6 +81,10 @@ class WebSocketsClient: private WebSockets {
|
|||||||
void setAuthorization(const char * user, const char * password);
|
void setAuthorization(const char * user, const char * password);
|
||||||
void setAuthorization(const char * auth);
|
void setAuthorization(const char * auth);
|
||||||
|
|
||||||
|
void setExtraHeaders(const char * extraHeaders = NULL);
|
||||||
|
|
||||||
|
void setReconnectInterval(unsigned long time);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
String _host;
|
String _host;
|
||||||
uint16_t _port;
|
uint16_t _port;
|
||||||
@ -87,7 +96,10 @@ class WebSocketsClient: private WebSockets {
|
|||||||
|
|
||||||
WebSocketClientEvent _cbEvent;
|
WebSocketClientEvent _cbEvent;
|
||||||
|
|
||||||
void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length);
|
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);
|
void clientDisconnect(WSclient_t * client);
|
||||||
bool clientIsConnected(WSclient_t * client);
|
bool clientIsConnected(WSclient_t * client);
|
||||||
|
@ -468,21 +468,24 @@ bool WebSocketsServer::newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient) {
|
|||||||
* @param client WSclient_t * ptr to the client struct
|
* @param client WSclient_t * ptr to the client struct
|
||||||
* @param opcode WSopcode_t
|
* @param opcode WSopcode_t
|
||||||
* @param payload uint8_t *
|
* @param payload uint8_t *
|
||||||
* @param lenght size_t
|
* @param length size_t
|
||||||
*/
|
*/
|
||||||
void WebSocketsServer::messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t lenght) {
|
void WebSocketsServer::messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin) {
|
||||||
WStype_t type = WStype_ERROR;
|
WStype_t type = WStype_ERROR;
|
||||||
|
|
||||||
switch(opcode) {
|
switch(opcode) {
|
||||||
case WSop_text:
|
case WSop_text:
|
||||||
type = WStype_TEXT;
|
type = fin ? WStype_TEXT : WStype_FRAGMENT_TEXT_START;
|
||||||
break;
|
break;
|
||||||
case WSop_binary:
|
case WSop_binary:
|
||||||
type = WStype_BIN;
|
type = fin ? WStype_BIN : WStype_FRAGMENT_BIN_START;
|
||||||
|
break;
|
||||||
|
case WSop_continuation:
|
||||||
|
type = fin ? WStype_FRAGMENT_FIN : WStype_FRAGMENT;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
runCbEvent(client->num, type, payload, lenght);
|
runCbEvent(client->num, type, payload, length);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -664,6 +667,7 @@ bool WebSocketsServer::hasMandatoryHeader(String headerName) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* handles http header reading for WebSocket upgrade
|
* handles http header reading for WebSocket upgrade
|
||||||
* @param client WSclient_t * ///< pointer to the client struct
|
* @param client WSclient_t * ///< pointer to the client struct
|
||||||
@ -671,6 +675,8 @@ bool WebSocketsServer::hasMandatoryHeader(String headerName) {
|
|||||||
*/
|
*/
|
||||||
void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
|
void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
|
||||||
|
|
||||||
|
static const char * NEW_LINE = "\r\n";
|
||||||
|
|
||||||
headerLine->trim(); // remove \r
|
headerLine->trim(); // remove \r
|
||||||
|
|
||||||
if(headerLine->length() > 0) {
|
if(headerLine->length() > 0) {
|
||||||
@ -690,25 +696,25 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
|
|||||||
String headerName = headerLine->substring(0, headerLine->indexOf(':'));
|
String headerName = headerLine->substring(0, headerLine->indexOf(':'));
|
||||||
String headerValue = headerLine->substring(headerLine->indexOf(':') + 2);
|
String headerValue = headerLine->substring(headerLine->indexOf(':') + 2);
|
||||||
|
|
||||||
if(headerName.equalsIgnoreCase("Connection")) {
|
if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Connection"))) {
|
||||||
headerValue.toLowerCase();
|
headerValue.toLowerCase();
|
||||||
if(headerValue.indexOf("upgrade") >= 0) {
|
if(headerValue.indexOf(WEBSOCKETS_STRING("upgrade")) >= 0) {
|
||||||
client->cIsUpgrade = true;
|
client->cIsUpgrade = true;
|
||||||
}
|
}
|
||||||
} else if(headerName.equalsIgnoreCase("Upgrade")) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Upgrade"))) {
|
||||||
if(headerValue.equalsIgnoreCase("websocket")) {
|
if(headerValue.equalsIgnoreCase(WEBSOCKETS_STRING("websocket"))) {
|
||||||
client->cIsWebsocket = true;
|
client->cIsWebsocket = true;
|
||||||
}
|
}
|
||||||
} else if(headerName.equalsIgnoreCase("Sec-WebSocket-Version")) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Version"))) {
|
||||||
client->cVersion = headerValue.toInt();
|
client->cVersion = headerValue.toInt();
|
||||||
} else if(headerName.equalsIgnoreCase("Sec-WebSocket-Key")) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Key"))) {
|
||||||
client->cKey = headerValue;
|
client->cKey = headerValue;
|
||||||
client->cKey.trim(); // see rfc6455
|
client->cKey.trim(); // see rfc6455
|
||||||
} else if(headerName.equalsIgnoreCase("Sec-WebSocket-Protocol")) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Protocol"))) {
|
||||||
client->cProtocol = headerValue;
|
client->cProtocol = headerValue;
|
||||||
} else if(headerName.equalsIgnoreCase("Sec-WebSocket-Extensions")) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Extensions"))) {
|
||||||
client->cExtensions = headerValue;
|
client->cExtensions = headerValue;
|
||||||
} else if(headerName.equalsIgnoreCase("Authorization")) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Authorization"))) {
|
||||||
client->base64Authorization = headerValue;
|
client->base64Authorization = headerValue;
|
||||||
} else {
|
} else {
|
||||||
client->cHttpHeadersValid &= execHttpHeaderValidation(headerName, headerValue);
|
client->cHttpHeadersValid &= execHttpHeaderValidation(headerName, headerValue);
|
||||||
@ -760,17 +766,13 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(_base64Authorization.length() > 0) {
|
if(_base64Authorization.length() > 0) {
|
||||||
if(client->base64Authorization.length() > 0) {
|
String auth = WEBSOCKETS_STRING("Basic ");
|
||||||
String auth = "Basic ";
|
|
||||||
auth += _base64Authorization;
|
auth += _base64Authorization;
|
||||||
if(auth != client->base64Authorization) {
|
if(auth != client->base64Authorization) {
|
||||||
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] HTTP Authorization failed!\n", client->num);
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] HTTP Authorization failed!\n", client->num);
|
||||||
handleAuthorizationFailed(client);
|
handleAuthorizationFailed(client);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
ok = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ok) {
|
if(ok) {
|
||||||
@ -784,32 +786,30 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
|
|||||||
|
|
||||||
client->status = WSC_CONNECTED;
|
client->status = WSC_CONNECTED;
|
||||||
|
|
||||||
client->tcp->write("HTTP/1.1 101 Switching Protocols\r\n"
|
String handshake = WEBSOCKETS_STRING("HTTP/1.1 101 Switching Protocols\r\n"
|
||||||
"Server: arduino-WebSocketsServer\r\n"
|
"Server: arduino-WebSocketsServer\r\n"
|
||||||
"Upgrade: websocket\r\n"
|
"Upgrade: websocket\r\n"
|
||||||
"Connection: Upgrade\r\n"
|
"Connection: Upgrade\r\n"
|
||||||
"Sec-WebSocket-Version: 13\r\n"
|
"Sec-WebSocket-Version: 13\r\n"
|
||||||
"Sec-WebSocket-Accept: ");
|
"Sec-WebSocket-Accept: ");
|
||||||
client->tcp->write((uint8_t*)sKey.c_str(), sKey.length());
|
handshake += sKey + NEW_LINE;
|
||||||
|
|
||||||
if(_origin.length() > 0) {
|
if(_origin.length() > 0) {
|
||||||
String origin = "\r\nAccess-Control-Allow-Origin: ";
|
handshake += WEBSOCKETS_STRING("Access-Control-Allow-Origin: ");
|
||||||
origin += _origin;
|
handshake +=_origin + NEW_LINE;
|
||||||
origin += "\r\n";
|
|
||||||
client->tcp->write((uint8_t*)origin.c_str(), origin.length());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(client->cProtocol.length() > 0) {
|
if(client->cProtocol.length() > 0) {
|
||||||
String protocol = "\r\nSec-WebSocket-Protocol: ";
|
handshake += WEBSOCKETS_STRING("Sec-WebSocket-Protocol: ");
|
||||||
protocol += _protocol;
|
handshake +=_protocol + NEW_LINE;
|
||||||
protocol += "\r\n";
|
|
||||||
client->tcp->write((uint8_t*)protocol.c_str(), protocol.length());
|
|
||||||
} else {
|
|
||||||
client->tcp->write("\r\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// header end
|
// header end
|
||||||
client->tcp->write("\r\n");
|
handshake += NEW_LINE;
|
||||||
|
|
||||||
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] handshake %s", client->num, (uint8_t*)handshake.c_str());
|
||||||
|
|
||||||
|
write(client, (uint8_t*)handshake.c_str(), handshake.length());
|
||||||
|
|
||||||
headerDone(client);
|
headerDone(client);
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ protected:
|
|||||||
|
|
||||||
bool newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient);
|
bool newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient);
|
||||||
|
|
||||||
void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length);
|
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);
|
||||||
bool clientIsConnected(WSclient_t * client);
|
bool clientIsConnected(WSclient_t * client);
|
||||||
@ -149,7 +149,6 @@ protected:
|
|||||||
* @param client WSclient_t * ptr to the client struct
|
* @param client WSclient_t * ptr to the client struct
|
||||||
*/
|
*/
|
||||||
virtual void handleAuthorizationFailed(WSclient_t *client) {
|
virtual void handleAuthorizationFailed(WSclient_t *client) {
|
||||||
|
|
||||||
client->tcp->write("HTTP/1.1 401 Unauthorized\r\n"
|
client->tcp->write("HTTP/1.1 401 Unauthorized\r\n"
|
||||||
"Server: arduino-WebSocket-Server\r\n"
|
"Server: arduino-WebSocket-Server\r\n"
|
||||||
"Content-Type: text/plain\r\n"
|
"Content-Type: text/plain\r\n"
|
||||||
|
24
travis/common.sh
Normal file
24
travis/common.sh
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
function build_sketches()
|
||||||
|
{
|
||||||
|
local arduino=$1
|
||||||
|
local srcpath=$2
|
||||||
|
local platform=$3
|
||||||
|
local sketches=$(find $srcpath -name *.ino)
|
||||||
|
for sketch in $sketches; do
|
||||||
|
local sketchdir=$(dirname $sketch)
|
||||||
|
if [[ -f "$sketchdir/.$platform.skip" ]]; then
|
||||||
|
echo -e "\n\n ------------ Skipping $sketch ------------ \n\n";
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
echo -e "\n\n ------------ Building $sketch ------------ \n\n";
|
||||||
|
$arduino --verify $sketch;
|
||||||
|
local result=$?
|
||||||
|
if [ $result -ne 0 ]; then
|
||||||
|
echo "Build failed ($1)"
|
||||||
|
return $result
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user