示例#1
0
class LudosWebSocketHandler(websocket.WebSocketHandler):
    def open(self):
        global CONNECTION_ID
        self.id = CONNECTION_ID
        CONNECTION_ID += 1
        logging.info('Connection made. Assigned connection ID %d.' % self.id)
        self.connection = LudosConnection()
        self.connection.bind('sendCommand', self.send_command)
        self.connection.bind('disconnect', self.disconnect)
        self.periodic()

    def periodic(self):
        self.connection.periodic()
        self.timeout = IOLoop.instance().add_timeout(timedelta(seconds=2), self.periodic)

    def send_command(self, command):
        logging.debug('%s -> %d' % (command, self.id))
        try:
            self.write_message(command_to_json(command))
        except AttributeError:
            pass

    def disconnect(self):
        self.close()

    def on_message(self, message):
        command = command_from_json(message)
        logging.debug('%d -> %s' % (self.id, command))
        self.connection.on_command(command)

    def on_close(self):
        logging.info('Connection %d closed.' % self.id)
        IOLoop.instance().remove_timeout(self.timeout)
        self.connection.on_disconnect(True)
示例#2
0
 def open(self):
     global CONNECTION_ID
     self.id = CONNECTION_ID
     CONNECTION_ID += 1
     logging.info('Connection made. Assigned connection ID %d.' % self.id)
     self.connection = LudosConnection()
     self.connection.bind('sendCommand', self.send_command)
     self.connection.bind('disconnect', self.disconnect)
     self.periodic()