Skip to content

Instantly share code, notes, and snippets.

@omaraboumrad
Created March 12, 2013 12:00
Show Gist options
  • Save omaraboumrad/5142357 to your computer and use it in GitHub Desktop.
Save omaraboumrad/5142357 to your computer and use it in GitHub Desktop.
python tornadoweb websocket sample
<html>
<head>
<title>Foo</title>
<script type="text/javascript">
var ws = new WebSocket("ws://localhost:8888/ws");
ws.onopen = function(){
ws.send("sup?");
};
ws.onmessage = function(evt){
alert(evt.data);
};
</script>
</head>
<body>
</body>
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
class EchoWebSocket(tornado.websocket.WebSocketHandler):
def open(self):
print "WebSocket opened"
def on_message(self, message):
self.write_message(u"You said: " + message)
def on_close(self):
print "WebSocket closed"
application = tornado.web.Application([
(r'/ws', EchoWebSocket),
])
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment