class WebSocket(tornado.websocket.WebSocketHandler):

    def initialize(self, **kwargs):
        print('connecting to mc ...')
        connection_string = kwargs['mc_ip']+':'+str(kwargs['mc_port'])
        self.mc_connection = Cache([connection_string])
        print('done')

    def open(self, *args, **kwargs):
        self.ws_connection.write_message('get sess_id')
        pass

    def on_message(self, message):
        try:
            request = json.loads(message)
            sess_id = request['sess_id']
            user_id = self.mc_connection.get_user_id('MR.sessId_', sess_id)
            if user_id != 0:
                ws_cp = WSConnectionPool()
                ws_cp.append(user_id, self)
                self.current_user = user_id
        except Exception as e:
            pass
        return 0

    def on_close(self):  # Garbage collector
        ws_cp = WSConnectionPool()
        if self.current_user:
            for connection_key in range(len(ws_cp[self.current_user])):
                if not ws_cp[self.current_user][connection_key].ws_connection:
                    del ws_cp[self.current_user][connection_key]
        del self

    def check_origin(self, origin):
        return True