From cd591f70d667cd7eab8785c14cceb7abc1316c95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Mon, 2 Jan 2017 15:51:45 +0100 Subject: [PATCH] Allow multi-packet websocket frames --- code/espurna/web.ino | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 1905d166..ed9c29ea 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -382,6 +382,8 @@ bool _wsAuth(AsyncWebSocketClient * client) { void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ + static uint8_t * message; + // Authorize #ifndef NOWSAUTH if (!_wsAuth(client)) return; @@ -398,7 +400,27 @@ void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTy } else if(type == WS_EVT_PONG) { DEBUG_MSG("[WEBSOCKET] #%u pong(%u): %s\n", client->id(), len, len ? (char*) data : ""); } else if(type == WS_EVT_DATA) { - _wsParse(client->id(), data, len); + + AwsFrameInfo * info = (AwsFrameInfo*)arg; + + // First packet + if (info->index == 0) { + //Serial.printf("Before malloc: %d\n", ESP.getFreeHeap()); + message = (uint8_t*) malloc(info->len); + //Serial.printf("After malloc: %d\n", ESP.getFreeHeap()); + } + + // Store data + memcpy(message + info->index, data, len); + + // Last packet + if (info->index + len == info->len) { + _wsParse(client->id(), message, info->len); + //Serial.printf("Before free: %d\n", ESP.getFreeHeap()); + free(message); + //Serial.printf("After free: %d\n", ESP.getFreeHeap()); + } + } }