How to get client connection object in tornado websocket, Python -
i using tornado websocket simple test code. in test code, want tornado.websocket.websockethandler
.
for example, used way below.
class convplayerinterface(object): class websockethandler(tornado.websocket.websockethandler): client = none queue = ipcqueue.ipcqueue() def open(self): print 'new connection' self.client = self #in simple code, handles 1 client. self.write_message("connection open") def on_message(self, message): self.queue.put(message) def on_close(self): print 'connection closed' def __init__(self, url='/ws'): self.application = tornado.web.application([(url, self.websockethandler),]) self.httpserver = tornado.httpserver.httpserver(self.application) self.httpserver.listen(8888) self.queue = self.websockethandler.queue self.ioloop = threading.thread(target = tornado.ioloop.ioloop.instance().start) def start(self): self.ioloop.start() def get(self): return self.queue.get() def put(self, command): self.websockethandler.client.write_message(command)
but point when calls self.websockethandler.client.write_message(command)
in put()
method, python says client
non type.
any advice?
and how used client connection handler object in tornado
?
in part of code
def put(self, command): self.websockethandler.client.write_message(command)
you accessing websockethandler class, not class member. , "client" attribute of websockethandler none, expected.
websockethandler instance created each request tornado accept, there can several websocket handlers simultaneously.
if want have handle 1 connection - can this:
class convplayerinterface(object): the_only_handler = none class websockethandler(tornado.websocket.websockethandler): client = none queue = ipcqueue.ipcqueue() def open(self): print 'new connection' convplayerinterface.the_only_handler = self self.write_message("connection open") def on_message(self, message): self.queue.put(message) def on_close(self): convplayerinterface.the_only_handler = none print 'connection closed' def __init__(self, url='/ws'): self.application = tornado.web.application([(url, self.websockethandler),]) self.httpserver = tornado.httpserver.httpserver(self.application) self.httpserver.listen(8888) self.queue = self.websockethandler.queue self.ioloop = threading.thread(target = tornado.ioloop.ioloop.instance().start) def start(self): self.ioloop.start() def get(self): return self.queue.get() def put(self, command): if self.the_only_handler not none self.the_only_handler.write_message(command)
Comments
Post a Comment