Skip to content

Instantly share code, notes, and snippets.

@laclefyoshi
Created November 26, 2010 13:25
Show Gist options
  • Save laclefyoshi/716700 to your computer and use it in GitHub Desktop.
Save laclefyoshi/716700 to your computer and use it in GitHub Desktop.
WebSocket server with Tornado
#!/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