diff --git a/examples/websocket-service/README.md b/examples/websocket-service/README.md new file mode 100644 index 0000000..1d6c58b --- /dev/null +++ b/examples/websocket-service/README.md @@ -0,0 +1,27 @@ +# Websocket Service + +## Setup + +```shell +script/setup --websockets +``` + +## Quick Start + +In one terminal, run the following + +```shell +script/run_websockets --uri 'tcp://0.0.0.0:10701' --websocket-host '0.0.0.0' +``` + +This will spawn a Wyoming server that listens to and then subsequently publishes events to the provided websocket host and port + +```shell +script/run --name 'my satellite' --uri 'tcp://0.0.0.0:10700' --mic-command 'arecord -r 16000 -c 1 -f S16_LE -t raw' --snd-command 'aplay -r 22050 -c 1 -f S16_LE -t raw' --event-uri 'tcp://0.0.0.0:10701' +``` + +In another terminal, run the above. This will start the actual satellite and publish events to the Wyoming server in Terminal 1. + +If you open the `timers.html` file in a browser, in the Network tab of the Dev Tools, you should see it connect to the websocket. + +Alternatively, if you have another app that uses websockets, you should be able to connect to that as well now \ No newline at end of file diff --git a/examples/websocket-service/server.py b/examples/websocket-service/server.py index 6fbf84c..b01b6eb 100644 --- a/examples/websocket-service/server.py +++ b/examples/websocket-service/server.py @@ -84,8 +84,8 @@ def __init__( self.queue = queue async def handle_event(self, event: Event) -> bool: - _LOGGER.debug(event) - self.queue.put_nowait(event) + _LOGGER.info(event) + await self.queue.put(event) return True diff --git a/examples/websocket-service/requirements.txt b/requirements_websockets.txt similarity index 100% rename from examples/websocket-service/requirements.txt rename to requirements_websockets.txt diff --git a/script/run_websockets b/script/run_websockets new file mode 100644 index 0000000..24604ad --- /dev/null +++ b/script/run_websockets @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import sys +import subprocess +import venv +from pathlib import Path + +_DIR = Path(__file__).parent +_PROGRAM_DIR = _DIR.parent +_VENV_DIR = _PROGRAM_DIR / ".venv" + +context = venv.EnvBuilder().ensure_directories(_VENV_DIR) +subprocess.check_call([context.env_exe, "examples/websocket-service/server.py"] + sys.argv[1:]) diff --git a/script/setup b/script/setup index 4928364..66956da 100755 --- a/script/setup +++ b/script/setup @@ -10,6 +10,7 @@ _VENV_DIR = _PROGRAM_DIR / ".venv" parser = argparse.ArgumentParser() parser.add_argument("--dev", action="store_true", help="Install dev requirements") +parser.add_argument("--websockets", action="store_true", help="Install websocket requirements") args = parser.parse_args() # Create virtual environment @@ -41,3 +42,9 @@ if args.dev: subprocess.check_call( pip + ["install", "-r", str(_PROGRAM_DIR / "requirements_dev.txt")] ) + +if args.websockets: + # Install websocket requirements + subprocess.check_call( + pip + ["install", "-r", str(_PROGRAM_DIR / "requirements_websockets.txt")] + )