From 244105b3ef167d4f93c4244e3e1c0bf48319e7c0 Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 24 Jun 2024 12:16:01 +0100 Subject: [PATCH] Minor websocket client issues (#2827) This PR adds a few fixes for websockets I seem to have kicking about. - Tidy, remove unused websocket strings - `wsserver` tool missing port argument handling, default is 8000 not 9999 **Check return value from http `connect()`** I can force this to fail by calling 'connect' twice in succession, though I do notice a disparity here. If the http connection is 'processing' then `WebsocketClient::connect` returns `false`; however, `HttpClientConnection::connect` returns `true` in this situation. --- Sming/Components/Network/component.mk | 2 +- .../Network/Http/Websocket/WebsocketClient.cpp | 4 +++- .../Http/Websocket/WebsocketConnection.cpp | 6 ------ .../Network/Http/Websocket/WebsocketConnection.h | 16 +++++----------- Sming/Components/Network/tools/wsserver.py | 10 +++++++--- 5 files changed, 16 insertions(+), 22 deletions(-) diff --git a/Sming/Components/Network/component.mk b/Sming/Components/Network/component.mk index 5763cdca7c..64872dd5d5 100644 --- a/Sming/Components/Network/component.mk +++ b/Sming/Components/Network/component.mk @@ -98,7 +98,7 @@ endif # Websocket Server CACHE_VARS += WSSERVER_PORT -WSSERVER_PORT ?= 9999 +WSSERVER_PORT ?= 8000 .PHONY: wsserver wsserver: ##Launch a simple python Websocket echo server for testing client applications $(info Starting Websocket server for TESTING) diff --git a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketClient.cpp b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketClient.cpp index 1f275a3098..178e38cd7b 100644 --- a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketClient.cpp +++ b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketClient.cpp @@ -58,7 +58,9 @@ bool WebsocketClient::connect(const Url& url) } httpConnection->setSslInitHandler(sslInitHandler); - httpConnection->connect(uri.Host, uri.getPort(), useSsl); + if(!httpConnection->connect(uri.Host, uri.getPort(), useSsl)) { + return false; + } state = eWSCS_Ready; diff --git a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp index 8e50ecd6d0..03193c410f 100644 --- a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp +++ b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp @@ -15,14 +15,8 @@ #include #include -DEFINE_FSTR(WSSTR_CONNECTION, "connection") DEFINE_FSTR(WSSTR_UPGRADE, "upgrade") DEFINE_FSTR(WSSTR_WEBSOCKET, "websocket") -DEFINE_FSTR(WSSTR_HOST, "host") -DEFINE_FSTR(WSSTR_ORIGIN, "origin") -DEFINE_FSTR(WSSTR_KEY, "Sec-WebSocket-Key") -DEFINE_FSTR(WSSTR_PROTOCOL, "Sec-WebSocket-Protocol") -DEFINE_FSTR(WSSTR_VERSION, "Sec-WebSocket-Version") DEFINE_FSTR(WSSTR_SECRET, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11") WebsocketList WebsocketConnection::websocketList; diff --git a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.h b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.h index 95f2f12620..1a3f7c865d 100644 --- a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.h +++ b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.h @@ -25,14 +25,8 @@ extern "C" { #define WEBSOCKET_VERSION 13 // 1.3 -DECLARE_FSTR(WSSTR_CONNECTION) DECLARE_FSTR(WSSTR_UPGRADE) DECLARE_FSTR(WSSTR_WEBSOCKET) -DECLARE_FSTR(WSSTR_HOST) -DECLARE_FSTR(WSSTR_ORIGIN) -DECLARE_FSTR(WSSTR_KEY) -DECLARE_FSTR(WSSTR_PROTOCOL) -DECLARE_FSTR(WSSTR_VERSION) DECLARE_FSTR(WSSTR_SECRET) class WebsocketConnection; @@ -292,11 +286,11 @@ class WebsocketConnection bool processFrame(TcpClient& client, char* at, int size); protected: - WebsocketDelegate wsConnect = nullptr; - WebsocketMessageDelegate wsMessage = nullptr; - WebsocketBinaryDelegate wsBinary = nullptr; - WebsocketDelegate wsPong = nullptr; - WebsocketDelegate wsDisconnect = nullptr; + WebsocketDelegate wsConnect; + WebsocketMessageDelegate wsMessage; + WebsocketBinaryDelegate wsBinary; + WebsocketDelegate wsPong; + WebsocketDelegate wsDisconnect; void* userData = nullptr; diff --git a/Sming/Components/Network/tools/wsserver.py b/Sming/Components/Network/tools/wsserver.py index f0f5aee8bd..a8c09f8b09 100644 --- a/Sming/Components/Network/tools/wsserver.py +++ b/Sming/Components/Network/tools/wsserver.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +import argparse import asyncio from websockets.server import serve import logging @@ -8,13 +9,16 @@ async def echo(websocket): async for message in websocket: await websocket.send(message) -async def main(): +async def main(port: int): logging.basicConfig( format="%(asctime)s %(message)s", level=logging.DEBUG, ) - async with serve(echo, None, 8000): + async with serve(ws_handler=echo, port=port): await asyncio.Future() # run forever if __name__ == "__main__": - asyncio.run(main()) + parser = argparse.ArgumentParser(description='Simple websocket server') + parser.add_argument('port', help='Port number (default 8000)', type=int, default=8000) + args = parser.parse_args() + asyncio.run(main(args.port))