WS Server
This commit is contained in:
4
Makefile
4
Makefile
@ -14,3 +14,7 @@ run-dev:
|
|||||||
--port 8010 \
|
--port 8010 \
|
||||||
--workers=1 \
|
--workers=1 \
|
||||||
--reload
|
--reload
|
||||||
|
|
||||||
|
run-ws:
|
||||||
|
@poetry run \
|
||||||
|
python ws_server.py
|
||||||
|
|||||||
@ -16,6 +16,12 @@ class InactivityWatchdogSettings:
|
|||||||
TICK_INTERVAL: int = 1 # seconds
|
TICK_INTERVAL: int = 1 # seconds
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class WSServerSettings:
|
||||||
|
HOST: str = "localhost"
|
||||||
|
PORT: int = 8010
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class DebugSettings:
|
class DebugSettings:
|
||||||
PRINT_BOARD: bool = False
|
PRINT_BOARD: bool = False
|
||||||
@ -26,4 +32,5 @@ class DebugSettings:
|
|||||||
class Settings:
|
class Settings:
|
||||||
board: BoardSettings
|
board: BoardSettings
|
||||||
inacivity_watchdog: InactivityWatchdogSettings
|
inacivity_watchdog: InactivityWatchdogSettings
|
||||||
|
ws_server: WSServerSettings
|
||||||
debug: Optional[DebugSettings] = None
|
debug: Optional[DebugSettings] = None
|
||||||
|
|||||||
@ -1,7 +1,13 @@
|
|||||||
from hopper.models.config import BoardSettings, InactivityWatchdogSettings, Settings
|
from hopper.models.config import (
|
||||||
|
BoardSettings,
|
||||||
|
InactivityWatchdogSettings,
|
||||||
|
Settings,
|
||||||
|
WSServerSettings,
|
||||||
|
)
|
||||||
|
|
||||||
settings = Settings(
|
settings = Settings(
|
||||||
board=BoardSettings(),
|
board=BoardSettings(),
|
||||||
inacivity_watchdog=InactivityWatchdogSettings(),
|
inacivity_watchdog=InactivityWatchdogSettings(),
|
||||||
|
ws_server=WSServerSettings(),
|
||||||
debug=None,
|
debug=None,
|
||||||
)
|
)
|
||||||
|
|||||||
51
ws_server.py
Normal file
51
ws_server.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
|
||||||
|
import websockets
|
||||||
|
from websockets import WebSocketServerProtocol, broadcast
|
||||||
|
|
||||||
|
from settings import settings
|
||||||
|
|
||||||
|
connected_clients = set()
|
||||||
|
|
||||||
|
|
||||||
|
def setup_logging() -> None:
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.DEBUG,
|
||||||
|
format="%(asctime)s %(levelname)s - %(message)s",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def ws_handler(websocket: WebSocketServerProtocol):
|
||||||
|
connected_clients.add(websocket)
|
||||||
|
logging.info(f"Add client: {websocket}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
async for message in websocket:
|
||||||
|
broadcast(connected_clients, message)
|
||||||
|
# await websocket.send(f"Are you talking to me? {message}")
|
||||||
|
finally:
|
||||||
|
connected_clients.remove(websocket)
|
||||||
|
logging.info(f"Remove client: {websocket}")
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
setup_logging()
|
||||||
|
|
||||||
|
logging.info(
|
||||||
|
f"Starting FairHopper Websockets Server on {settings.ws_server.HOST}:{settings.ws_server.PORT}"
|
||||||
|
)
|
||||||
|
|
||||||
|
async with websockets.serve(
|
||||||
|
ws_handler=ws_handler,
|
||||||
|
host=settings.ws_server.HOST,
|
||||||
|
port=settings.ws_server.PORT,
|
||||||
|
):
|
||||||
|
await asyncio.Future() # run forever
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
asyncio.run(main())
|
||||||
|
except (KeyboardInterrupt, SystemExit):
|
||||||
|
logging.info(f"FairHopper Websockets Server terminated")
|
||||||
Reference in New Issue
Block a user