Skip to content

Commit

Permalink
Minor websocket client issues (#2827)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mikee47 authored Jun 24, 2024
1 parent cb8adf4 commit 244105b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Sming/Components/Network/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,8 @@
#include <Data/Stream/XorOutputStream.h>
#include <Data/Stream/SharedMemoryStream.h>

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
10 changes: 7 additions & 3 deletions Sming/Components/Network/tools/wsserver.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

import argparse
import asyncio
from websockets.server import serve
import logging
Expand All @@ -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))

0 comments on commit 244105b

Please sign in to comment.