Created
March 12, 2013 12:00
-
-
Save omaraboumrad/5142357 to your computer and use it in GitHub Desktop.
python tornadoweb websocket sample
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
<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> | |
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.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