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::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); }
err_t WebsocketClient::onReceive(pbuf* buf) { if (buf == NULL) { // Disconnected, close it TcpClient::onReceive(buf); } else { uint16_t size = buf->tot_len; uint8_t* data = new uint8_t[size]; pbuf_copy_partial(buf, data, size, 0); switch (_mode) { case wsMode::Connecting: if (_verifyKey((char*)data, size) == true) { _mode = wsMode::Connected; // debugf("Key Verified. Websocket Handshake completed"); sendPing(); } else { _mode = wsMode::Disconnected; // Handshake was not proper. } if (wsConnect) { wsConnect(*this, _mode); } break; case wsMode::Connected: WebsocketFrameClass wsFrame; do { if (wsFrame.decodeFrame(data, size)) { switch (wsFrame._frameType) { case WSFrameType::text: { // debugf("Got text frame"); String msg; msg.setString((char*)wsFrame._payload, wsFrame._payloadLength); if (wsMessage) { wsMessage(*this, msg.c_str()); } break; } case WSFrameType::binary: { // debugf("Got binary frame"); if (wsBinary) { wsBinary(*this, wsFrame._payload, wsFrame._payloadLength); } break; } case WSFrameType::close: { debugf("Got Disconnect request from server.\n"); //RFC requires we return a close op code before closing the connection disconnect(); break; } case WSFrameType::ping: { debugf("Got ping ..."); sendPong(); //Need to send Pong in response to Ping break; } case WSFrameType::pong: { debugf("Got pong ..."); //A pong can contain app data, but shouldnt if we didnt send any... break; } case WSFrameType::error: { debugf("ERROR parsing frame!"); break; } case WSFrameType::incomplete: { debugf("INCOMPLETE websocket frame!"); break; } default: { debugf("Unknown frameType: %d", wsFrame._frameType); break; } } } } while (wsFrame._nextReadOffset > 0); break; } delete[] data; TcpClient::onReceive(buf); } }