basic websockets server example

This commit is contained in:
2024-04-22 21:03:32 -04:00
parent 769adaf644
commit ca857c8d09
9 changed files with 96 additions and 8 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.venv/

View File

@@ -0,0 +1,34 @@
import websockets
import asyncio
# Server data
PORT = 7890
print("Server listening on Port " + str(PORT))
# A set of connected ws clients
connected = set()
# The main behavior function for this server
async def echo(websocket, path):
print("A client just connected")
print(f"path: {path}")
# Store a copy of the connected client
connected.add(websocket)
# Handle incoming messages
try:
async for message in websocket:
print("Received message from client: " + message)
# Send a response to all connected clients except sender
for conn in connected:
if conn != websocket:
await conn.send("Someone said: " + message)
# Handle disconnecting clients
except websockets.exceptions.ConnectionClosed as e:
print("A client just disconnected")
finally:
connected.remove(websocket)
# Start the server
start_server = websockets.serve(echo, "0.0.0.0", PORT)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

View File

@@ -13,16 +13,12 @@ readme = "README.md"
requires-python = ">=3.8"
classifiers = []
dependencies = [
"pydantic==1.10.2",
"scipy==1.8.1",
# These should be included in the above requirement
# "marshmallow>=3.3.0",
# "marshmallow_dataclass>=7.2.1",
# "marshmallow_oneofschema>=2.0.1",
# "pydantic==1.10.2",
"pojagi-dsp==0.0.0.dev0",
# "PyYAML>=5.3.1",
"websockets==12.0",
]
# optional-dependencies = { test = ["pytest"] }
version = "0.0.0.dev0"
# dynamic = ["version"]

View File

@@ -0,0 +1,10 @@
Metadata-Version: 2.1
Name: medtrace-synth
Version: 0.0.0.dev0
Summary: Synthesized biometric signals service
Author-email: Tom Brennan <tjb1982@gmail.com>
Project-URL: gitlab, https://gitlab.pojagi.org/tjb1982/medtrace-synth
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Requires-Dist: pojagi-dsp==0.0.0.dev0
Requires-Dist: websockets==12.0

View File

@@ -0,0 +1,7 @@
pyproject.toml
src/medtrace_synth/__main__.py
src/medtrace_synth.egg-info/PKG-INFO
src/medtrace_synth.egg-info/SOURCES.txt
src/medtrace_synth.egg-info/dependency_links.txt
src/medtrace_synth.egg-info/requires.txt
src/medtrace_synth.egg-info/top_level.txt

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,2 @@
pojagi-dsp==0.0.0.dev0
websockets==12.0

View File

@@ -0,0 +1 @@
medtrace_synth

View File

@@ -0,0 +1,36 @@
import websockets
import asyncio
# Server data
PORT = 7890
print("Server listening on Port " + str(PORT))
# A set of connected ws clients
connected = set()
# The main behavior function for this server
async def echo(websocket, path):
print("A client just connected")
print(f"path: {path}")
# Store a copy of the connected client
connected.add(websocket)
# Handle incoming messages
try:
async for message in websocket:
print("Received message from client: " + message)
# Send a response to all connected clients except sender
for conn in connected:
if conn != websocket:
await conn.send("Someone said: " + message)
else:
await conn.send(f"You said: {message}")
# Handle disconnecting clients
except websockets.exceptions.ConnectionClosed as e:
print("A client just disconnected")
finally:
connected.remove(websocket)
# Start the server
start_server = websockets.serve(echo, "0.0.0.0", PORT)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()