home · contact · privacy
49bdb77cf5ba6609c05f43bfd3894b1fa837e6d9
[plomrogue2-experiments] / new2 / 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     
16     def handleConnected(self):
17         import uuid
18         print('CONNECTION FROM:', self.address)
19         connection_id = uuid.uuid4()
20         self.server.clients[connection_id] = self
21
22     def handleClose(self):
23         print('CONNECTION CLOSED FROM:', self.address)
24         for connection_ids in self.server.clients:
25             if self.server.clients[connection_id] == self:
26                 del self.server.clients[connection_id]
27
28     def put(self, msg):
29         self.sendMessage(msg)
30
31
32
33 class PlomWebSocketServer(SimpleWebSocketServer):
34
35     def __init__(self, queue, port, *args, **kwargs):
36         super().__init__('', port, PlomWebSocket)
37         self.queue = queue
38         self.clients = {}
39
40     def serve_forever(self):
41         self.serveforever()
42
43     def server_close(self):
44         self.close()
45