home · contact · privacy
Fix broken map geometry commandeering.
[plomrogue2] / plomrogue / io_websocket.py
1 from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
2
3
4
5 class PlomWebSocket(WebSocket):
6
7     def handleMessage(self):
8         if self.data == 'QUIT':
9             self.sendMessage('BYE')
10             self.close()
11         else:
12             for connection_id in self.server.clients:
13                 if self.server.clients[connection_id] == self:
14                     self.server.queue.put((connection_id, self.data))
15                     break
16
17     def handleConnected(self):
18         import uuid
19         print('CONNECTION FROM:', self.address)
20         connection_id = uuid.uuid4()
21         self.server.clients[connection_id] = self
22
23     def handleClose(self):
24         print('CONNECTION CLOSED FROM:', self.address)
25         for connection_id in self.server.clients:
26             if self.server.clients[connection_id] == self:
27                 del self.server.clients[connection_id]
28
29     def put(self, msg):
30         self.sendMessage(msg)
31
32
33
34 class PlomWebSocketServer(SimpleWebSocketServer):
35
36     def __init__(self, queue, port, *args, **kwargs):
37         super().__init__('', port, PlomWebSocket)
38         self.queue = queue
39         self.clients = {}
40
41     def serve_forever(self):
42         self.serveforever()
43
44     def server_close(self):
45         self.close()