-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket_server.py
37 lines (29 loc) · 1.41 KB
/
websocket_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
28
29
30
31
32
33
34
35
36
37
import asyncio
import json
import websockets
async def handle_undefined_packet(websocket, packet):
print("undefined command", packet["command"])
class WebsocketServer:
def __init__(self, open_handler, close_handler, packet_handlers):
self.packet_handlers = packet_handlers
self.close_handler = close_handler
self.open_handler = open_handler
async def websocket_handler(self, websocket, path):
print("Client connection at " + websocket.remote_address[0])
await self.open_handler(websocket)
try:
while True:
data = await websocket.recv()
packet = json.loads(data)
handler = self.packet_handlers.get(packet["command"], handle_undefined_packet)
await handler(websocket, packet)
except websockets.ConnectionClosed:
print("Connection closed at " + websocket.remote_address[0])
await self.close_handler(websocket)
def start_websocket(host, port, open_handler, close_handler, packet_handlers):
wserver = WebsocketServer(open_handler, close_handler, packet_handlers)
asyncio.set_event_loop(asyncio.new_event_loop())
start_server = websockets.serve(wserver.websocket_handler, host, port)
asyncio.get_event_loop().run_until_complete(start_server)
print("Websocket started at ws://" + host + ":" + str(port) + "/")
asyncio.get_event_loop().run_forever()