void HttpServer::processWebSocketFrame(pbuf *buf, HttpServerConnection& connection) { //TODO: process splitted payload uint8_t* data; size_t size; wsFrameType frameType = wsParseInputFrame((uint8_t*)buf->payload, buf->len, &data, &size); WebSocket* sock = getWebSocket(connection); if (frameType == WS_TEXT_FRAME) { String msg; msg.setString((char*)data, size); debugf("WS: %s", msg.c_str()); if (sock && wsMessage) wsMessage(*sock, msg); } if (frameType == WS_BINARY_FRAME) { if (sock && wsMessage) wsBinary(*sock, data, size); } else if (frameType == WS_CLOSING_FRAME) { connection.close(); // it will be processed automatically in onCloseWebSocket callback } else if (frameType == WS_INCOMPLETE_FRAME || frameType == WS_ERROR_FRAME) debugf("WS error reading frame: %X", frameType); else debugf("WS frame type: %X", frameType); }
void HttpServer::onCloseWebSocket(HttpServerConnection& connection) { WebSocket* sock = getWebSocket(connection); removeWebSocket(connection); debugf("WS Close"); if (sock && wsDisconnect) wsDisconnect(*sock); }
void HttpServer::processWebSocketFrame(pbuf *buf, HttpServerConnection& connection) { //TODO: process splitted payload uint8_t* data; size_t size; wsFrameType frameType = (wsFrameType) 0x01; uint8_t payloadFieldExtraBytes = 0; size_t payloadLength = 0; size_t payloadShift = 0; do { payloadLength = getPayloadLength((uint8_t*)buf->payload + payloadShift, buf->len, &payloadFieldExtraBytes, &frameType); // debugf("payloadLength: %u, payLoadShift: %u, payloadFieldExtraBytes: %u\n", payloadLength, payloadShift, payloadFieldExtraBytes); wsFrameType frameType = wsParseInputFrame((uint8_t*)buf->payload + payloadShift, (payloadLength + 6 + payloadFieldExtraBytes), &data, &size); WebSocket* sock = getWebSocket(connection); if (frameType == WS_TEXT_FRAME) { String msg; msg.setString((char*)data, size); debugf("WS: %s", msg.c_str()); if (sock && wsMessage) wsMessage(*sock, msg); #if ENABLE_CMD_EXECUTOR if (sock && sock->commandExecutor) sock->commandExecutor->executorReceive(msg+"\r"); #endif } else if (frameType == WS_BINARY_FRAME) { if (sock && wsMessage) wsBinary(*sock, data, size); } else if (frameType == WS_CLOSING_FRAME) { connection.close(); // it will be processed automatically in onCloseWebSocket callback } else if (frameType == WS_INCOMPLETE_FRAME || frameType == WS_ERROR_FRAME) debugf("WS error reading frame: %X", frameType); else debugf("WS frame type: %X", frameType); payloadShift += payloadLength + 6 + payloadFieldExtraBytes; } while (buf->len > payloadShift); }