Moving forward on websockets
This commit is contained in:
@ -1,25 +1,31 @@
|
||||
import asyncio
|
||||
import websockets
|
||||
import json
|
||||
import time
|
||||
|
||||
connected_clients = set()
|
||||
validTokens = "123456"
|
||||
|
||||
def sendData(websocket):
|
||||
while True:
|
||||
websocket.send("Heartbeat")
|
||||
print("Heartbeat send")
|
||||
time.sleep(5)
|
||||
|
||||
async def handler(websocket, path):
|
||||
connected_clients.add(websocket)
|
||||
try:
|
||||
print("New client connected to the server")
|
||||
if path != "/":
|
||||
print("client disconnected")
|
||||
await websocket.send(json.dumps({"error": "Invalid path", "code": 9000}))
|
||||
await websocket.close()
|
||||
return
|
||||
connected_clients.add(websocket)
|
||||
try :
|
||||
async for message in websocket:
|
||||
print(f"Received {message}")
|
||||
# for client in connected_clients:
|
||||
# if client == websocket:
|
||||
# print(f"Received {message}")
|
||||
# await websocket.close()
|
||||
# if client != websocket:
|
||||
# await client.send(message)
|
||||
print(f"Message reçu : {message}")
|
||||
except websockets.exceptions.ConnectionClosed as e:
|
||||
print(f"Connexion fermée: {e}")
|
||||
finally:
|
||||
print("Client déconnecté")
|
||||
connected_clients.remove(websocket)
|
||||
print("Client disconnected with error :", e)
|
||||
sendData(websocket)
|
||||
|
||||
try:
|
||||
start_server = websockets.serve(handler, "localhost", 8000, reuse_address=True)
|
||||
@ -27,4 +33,5 @@ except OSError as e:
|
||||
print(f"Error: {e}")
|
||||
exit(1)
|
||||
asyncio.get_event_loop().run_until_complete(start_server)
|
||||
print("Server started")
|
||||
asyncio.get_event_loop().run_forever()
|
Reference in New Issue
Block a user