Files
Telereview/code/backend_reconnaissance/network.py
Quentin Roussel 0d5167db57 restructuration du backend
préparation de l'intégration des modules suivants
2023-03-22 22:10:58 +01:00

31 lines
907 B
Python

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)