Search code examples
websockettornado

AttributeError: 'Application' object has no attribute 'webSocketsPool'


i wanto self.webSocketsPool = [] in my code because i have error AttributeError: 'Application' object has no attribute 'webSocketsPool'. But i don't know where i need to paste this in my tornado app.

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
        (r"/dishtypes/(.*)", DishTypesHandler),
        (r"/test", testhandler),
        (r"/user/([0-9]+)", UserProfile),
        (r"/login", LoginHandler),
        (r"/testcookie", testcoockie),
        (r"/registration", registrationHandler),
        (r"/authentication", authHandler),
        (r"/remc", removecockie),
        (r"/adddish", addDish),
        (r"/getusrbasket", getUserBasket),
        (r"/payorder", payOrder),
        (r"/clientorders", clientOrdersHandler),
        (r"/clientorders/([0-9]+)", clientOrdersIdHandler),
        (r'/websocket/?', WebSocket),

    ], **settings,
        template_path=os.path.join(os.path.dirname(__file__), "templates"),
        static_path=os.path.join(os.path.dirname(__file__), "static"),
        default_handler_class=NotFoundHandler)


if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

error line is on this class

class WebSocket(tornado.websocket.WebSocketHandler):
    def open(self):
        self.application.webSocketsPool.append(self)

    def on_message(self, message):
        db = self.application.db
        message_dict = json.loads(message);
        db.chat.insert(message_dict)
        for key, value in enumerate(self.application.webSocketsPool):
            if value != self:
                value.ws_connection.write_message(message)

    def on_close(self, message=None):
        for key, value in enumerate(self.application.webSocketsPool):
            if value == self:
                del self.application.webSocketsPool[key]

Solution

  • You can attach the list to the application object after creating it:

    if __name__ == "__main__":
        app = make_app()
        app.webSocketsPool = []
        app.listen(8888)
        tornado.ioloop.IOLoop.current().start()
    

    However you should consider a set instead of a list for performance reasons.