python - tornado how to use WebSockets with wsgi -
i trying make game server python, using tornado.
the problem websockets don't seem work wsgi.
wsgi_app = tornado.wsgi.wsgiadapter(app) server = wsgiref.simple_server.make_server('', 5000, wsgi_app) server.serve_forever()
after looking trough answer on stackoverflow, running tornado in apache, i've updated code use httpserver
, works websockets.
server = tornado.httpserver.httpserver(app) server.listen(5000) tornado.ioloop.ioloop.instance().start()
however when use httpserver, error comes saying: typeerror: __call__() takes 2 arguments (3 given)
looking on internet, found answer question here: tornado.wsgi.wsgiapplication issue: __call__ takes 3 arguments (2 given)
but after adding tornado.wsgi.wsgicontainer
around app
, error persists.
how can solve problem? or there way use tornado web sockets wsgi.
here code @ moment:
import tornado.web import tornado.websocket import tornado.wsgi import tornado.template import tornado.httpserver #import wsgiref.simple_server import wsgiref print "setting environment..." class tornadoapp(tornado.web.application): def __init__(self): handlers = [ (r"/chat", chatpagehandler), (r"/wschat", chatwshandler), (r"/*", mainhandler) ] settings = { "debug" : true, "template_path" : "templates", "static_path" : "static" } tornado.web.application.__init__(self, handlers, **settings) class mainhandler(tornado.web.requesthandler): def get(self): self.render("test.html", food = "pizza") class chatpagehandler(tornado.web.requesthandler): def get(self): self.render("chat.html") class chatwshandler(tornado.websocket.websockethandler): connections = [] def open(self, *args): print 'a' self.connections.append(self) print 'b' self.write_message(u"@server: websocket opened!") print 'c' def on_message(self, message): [con.write_message(u""+message) con in self.connections] def on_close(self): self.connections.remove(self) print "done" if __name__ == "__main__": app = tornadoapp() server = tornado.httpserver.httpserver(tornado.wsgi.wsgicontainer(app)) server.listen(5000) tornado.ioloop.ioloop.instance().start()
thank's in advace! appreciated.
httpserver
needs tornado.web.application
, not same wsgi application (which use wsgicontainer
).
it recommended if need both wsgi , websockets run them in 2 separate processes ipc mechanism of choice between them (and use real wsgi server instead of tornado's wsgicontainer
). if need them in same process, can use fallbackhandler
.
wsgi_container = tornado.wsgi.wsgicontainer(my_wsgi_app) application = tornado.web.application([ (r"/ws", mywebsockethandler), (r".*", fallbackhandler, dict(fallback=wsgi_container), ])
Comments
Post a Comment