mirror of
https://git.roussel.pro/telecom-paris/pact.git
synced 2026-02-09 02:20:17 +01:00
restructuration du backend
préparation de l'intégration des modules suivants
This commit is contained in:
31
code/backend_reconnaissance/network.py
Normal file
31
code/backend_reconnaissance/network.py
Normal 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)
|
||||
Reference in New Issue
Block a user