restructuration du backend

préparation de l'intégration des modules suivants
This commit is contained in:
Quentin Roussel
2023-03-22 22:10:58 +01:00
parent d896767543
commit 0d5167db57
8 changed files with 164 additions and 338 deletions

View File

@@ -0,0 +1,31 @@
import asyncio
import json
import os
import threading
import websockets
class WebsocketServer(threading.Thread):
def __init__(self, onMessage, port=os.getenv("PORT"), host=os.getenv("HOST")):
threading.Thread.__init__(self)
self.host = host
self.port = port
self.messageQueue = []
self.onMessage = onMessage
def run(self):
print("server thread started")
asyncio.run(self.runServer())
async def runServer(self):
async with websockets.serve(self.handler, self.host, self.port):
await asyncio.Future()
async def handler(self,websocket):
while True:
for msg in self.messageQueue:
await websocket.send(json.dumps(msg))
self.messageQueue.pop(0)
await asyncio.sleep(0.01)
def sendMessage(self,message):
self.messageQueue.append(message)