add support for Fragmentation / continuation opcode Receive

This commit is contained in:
Links
2017-02-22 14:29:26 +01:00
parent 6da0bc97e8
commit e93a323e56
8 changed files with 137 additions and 35 deletions

View File

@ -3,47 +3,47 @@ WebSocket Server and Client for Arduino
a WebSocket Server and Client for Arduino based on RFC6455. a WebSocket Server and Client for Arduino based on RFC6455.
##### Supported features of RFC6455 ##### ##### Supported features of RFC6455 #####
- text frame - text frame
- binary frame - binary frame
- 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.
- wss / SSL is not possible. - wss / SSL is not possible.
##### Supported Hardware ##### ##### Supported Hardware #####
- ESP8266 [Arduino for ESP8266](https://github.com/Links2004/Arduino) - ESP8266 [Arduino for ESP8266](https://github.com/Links2004/Arduino)
- ESP31B - ESP31B
- ATmega328 with Ethernet Shield (ATmega branch) - Particle with STM32 ARM Cortex M3
- ATmega328 with enc28j60 (ATmega branch) - ATmega328 with Ethernet Shield (ATmega branch)
- ATmega2560 with Ethernet Shield (ATmega branch) - ATmega328 with enc28j60 (ATmega branch)
- ATmega2560 with enc28j60 (ATmega branch) - ATmega2560 with Ethernet Shield (ATmega branch)
- ATmega2560 with enc28j60 (ATmega branch)
###### Note: ###### ###### Note: ######
version 2.0 and up is not compatible with AVR/ATmega, check ATmega branch. version 2.0 and up is not compatible with AVR/ATmega, check ATmega branch.
Arduino for AVR not supports std namespace of c++. Arduino for AVR not supports std namespace of c++.
### wss / SSL ### ### wss / SSL ###
supported for: supported for:
- wss client on the ESP8266 - wss client on the ESP8266
- wss / SSL is not natively supported in WebSocketsServer however it is possible to achieve secure websockets - wss / SSL is not natively supported in WebSocketsServer however it is possible to achieve secure websockets
by running the device behind an SSL proxy. See [Nginx](examples/Nginx/esp8266.ssl.reverse.proxy.conf) for a by running the device behind an SSL proxy. See [Nginx](examples/Nginx/esp8266.ssl.reverse.proxy.conf) for a
sample Nginx server configuration file to enable this. sample Nginx server configuration file to enable this.
### ESP Async TCP ### ### ESP Async TCP ###
This libary can run in Async TCP mode on the ESP. This libary can run in Async TCP mode on the ESP.
@ -52,9 +52,6 @@ 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).
- Use Particle's TCPClient and TCPServer classes instead of Arduino's.
### Issues ### ### Issues ###
Submit issues to: https://github.com/Links2004/arduinoWebSockets/issues Submit issues to: https://github.com/Links2004/arduinoWebSockets/issues

View File

@ -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 lenght) {
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 lenght: %u\n", num, lenght);
hexdump(payload, lenght);
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();
}

View File

@ -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;
@ -449,11 +450,7 @@ 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;

View File

@ -32,7 +32,11 @@
#include <Arduino.h> #include <Arduino.h>
#endif #endif
#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
#ifndef DEBUG_WEBSOCKETS #ifndef DEBUG_WEBSOCKETS
#define DEBUG_WEBSOCKETS(...) #define DEBUG_WEBSOCKETS(...)
@ -142,7 +146,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 {
@ -227,7 +235,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);

View File

@ -272,16 +272,19 @@ void WebSocketsClient::setAuthorization(const char * auth) {
* @param payload uint8_t * * @param payload uint8_t *
* @param lenght size_t * @param lenght 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 lenght, 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; break;
case WSop_continuation:
type = fin ? WStype_FRAGMENT_FIN : WStype_FRAGMENT;
break;
} }
runCbEvent(type, payload, lenght); runCbEvent(type, payload, lenght);

View File

@ -87,7 +87,7 @@ class WebSocketsClient: private WebSockets {
WebSocketClientEvent _cbEvent; WebSocketClientEvent _cbEvent;
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);

View File

@ -470,15 +470,18 @@ bool WebSocketsServer::newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient) {
* @param payload uint8_t * * @param payload uint8_t *
* @param lenght size_t * @param lenght 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 lenght, 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;
} }

View File

@ -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);