Created
November 26, 2010 13:25
-
-
Save laclefyoshi/716700 to your computer and use it in GitHub Desktop.
WebSocket server with 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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# Copyright : (c) SAEKI Yoshiyasu | |
# License : MIT-style license | |
# <http://www.opensource.org/licenses/mit-license.php> | |
# last updated: 2010/11/25 | |
import tornado.httpserver | |
import tornado.ioloop | |
import tornado.web | |
import tornado.websocket | |
connections = [] | |
class Balls(tornado.websocket.WebSocketHandler): | |
def open(self): | |
print("WebSocket opened") | |
if not (self in connections): | |
connections.append(self) | |
def on_message(self, data): | |
for con in connections: | |
if con != self: | |
print data | |
con.write_message(data) | |
def on_close(self): | |
print("WebSocket closed") | |
if self in connections: | |
connections.remove(self) | |
application = tornado.web.Application([ | |
(r"/balls", Balls), | |
]) | |
def main(): | |
http_server = tornado.httpserver.HTTPServer(application) | |
http_server.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment