basic websockets server example
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.venv/
|
||||
34
build/lib/medtrace_synth/__main__.py
Normal file
34
build/lib/medtrace_synth/__main__.py
Normal 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()
|
||||
@@ -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"]
|
||||
10
src/medtrace_synth.egg-info/PKG-INFO
Normal file
10
src/medtrace_synth.egg-info/PKG-INFO
Normal 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
|
||||
7
src/medtrace_synth.egg-info/SOURCES.txt
Normal file
7
src/medtrace_synth.egg-info/SOURCES.txt
Normal 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
|
||||
1
src/medtrace_synth.egg-info/dependency_links.txt
Normal file
1
src/medtrace_synth.egg-info/dependency_links.txt
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
2
src/medtrace_synth.egg-info/requires.txt
Normal file
2
src/medtrace_synth.egg-info/requires.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
pojagi-dsp==0.0.0.dev0
|
||||
websockets==12.0
|
||||
1
src/medtrace_synth.egg-info/top_level.txt
Normal file
1
src/medtrace_synth.egg-info/top_level.txt
Normal file
@@ -0,0 +1 @@
|
||||
medtrace_synth
|
||||
36
src/medtrace_synth/__main__.py
Normal file
36
src/medtrace_synth/__main__.py
Normal 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()
|
||||
Reference in New Issue
Block a user