-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
27 lines (23 loc) · 777 Bytes
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import websockets
import asyncio
# Server data
PORT = 8765
print("Server listening on Port " + str(PORT))
connected = set()
async def echo(websocket, path):
print("A client just connected")
connected.add(websocket)
try:
async for message in websocket:
print("Received message from client: " + message)
for conn in connected:
if conn != websocket:
await conn.send(message)
except websockets.exceptions.ConnectionClosed as e:
print("A client just disconnected")
finally:
connected.remove(websocket)
# Start the server
start_server = websockets.serve(echo, "localhost", PORT)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()