def start_websocket():
    class SocketHandler(WebSocket):
        def handleMessage(self):
            global _answer, _mouse_pos
            #print(self.data)
            with _cond:
                args = self.data.split(" ", 1)
                if args[0] == "answer":
                    _answer = args[1]
                    _cond.notify_all()
                elif args[0] == "mousemove":
                    _mouse_pos = tuple(map(int, args[1].split(" ")))
                elif args[0] == "keydown":
                    if args[1] in _released: _released.discard(args[1])
                    else: _pressed.add(args[1])
                elif args[0] == "keyup":
                    if args[1] in _pressed: _pressed.discard(args[1])
                    else: _released.add(args[1])

        def handleConnected(self):
            global _ws
            with _cond:
                _ws = self
                _cond.notify_all()

        def handleClose(self):
            global _ws
            self.server.closing = True
            self.server.close()
            with _cond:
                _ws = None

    server = SimpleWebSocketServer("localhost", _ws_port, SocketHandler)
    server.closing = False
    while not server.closing:
        server.serveonce()
Beispiel #2
0
def start_websocket():
    server = SimpleWebSocketServer("localhost", 7574, SocketHandler)
    server.closing = False
    while not server.closing:
        server.serveonce()