Created
March 27, 2015 20:14
-
-
Save paitonic/47ab5d3a4537f2e1f454 to your computer and use it in GitHub Desktop.
Websockets using Tornado
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.websocket | |
import json | |
class WebSocketHandler(tornado.websocket.WebSocketHandler): | |
clients = [] | |
def open(self): | |
WebSocketHandler.clients.append(self) | |
print "WebSocket opened" | |
def on_message(self, message): | |
self.write_message('onmessage!') | |
print "Received: ", message | |
def on_close(self): | |
WebSocketHandler.clients.remove(self) | |
print "WebSocket closed" | |
class IndexPageHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.render("index.html") | |
application = tornado.web.Application([ | |
(r"/websocket", WebSocketHandler), | |
(r"/", IndexPageHandler), | |
(r"/static/(.*)", tornado.web.StaticFileHandler, {'path': '/home/wowa/dev/ome-gui/static/'}) | |
], debug=True) | |
if __name__ == "__main__": | |
print "Starting server..." | |
application.listen(8888) | |
ioloop = tornado.websocket.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment