Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor websocket client issues #2827

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Loading