-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
27 lines (23 loc) · 929 Bytes
/
server.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 asyncio
import websockets
connected_clients = set()
async def handler(websocket, path):
connected_clients.add(websocket)
print(f"Client connected: {websocket.remote_address}")
try:
async for message in websocket:
# Broadcast the received message to all connected clients
for client in connected_clients:
if client != websocket:
await client.send(message)
print("Frame broadcasted to client")
except websockets.ConnectionClosed:
print(f"Client disconnected: {websocket.remote_address}")
finally:
connected_clients.remove(websocket)
async def main():
async with websockets.serve(handler, "26.248.111.145", 3306):
print("Server started on ws://localhost:8766")
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())