Skip to content

Instantly share code, notes, and snippets.

@chrisallick
Created September 22, 2012 08:54
Show Gist options
  • Save chrisallick/3765584 to your computer and use it in GitHub Desktop.
Save chrisallick/3765584 to your computer and use it in GitHub Desktop.
main_wss.py
import logging
import tornado.escape
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.websocket
import os.path
from tornado.options import define, options
define( "port", default=55362, help="run on the given port", type=int )
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/websocket", OSCWebSocketHandler),
]
settings = dict(
cookie_secret="43oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
xsrf_cookies=True,
autoescape=None,
)
tornado.web.Application.__init__(self, handlers, **settings)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
class OSCWebSocketHandler(tornado.websocket.WebSocketHandler):
waiters = set()
def open(self):
#OSCWebSocketHandler.waiters.add(self)
print "open"
def on_close(self):
print "closed"
#OSCWebSocketHandler.waiters.remove(self)
@classmethod
def send_updates(cls, msg, me ):
for waiter in cls.waiters:
try:
if waiter != me:
waiter.write_message( msg )
except:
logging.error("Error sending message", exc_info=True)
def on_message(self, message):
OSCWebSocketHandler.send_updates(message, self )
def main():
tornado.options.parse_command_line()
app = Application()
app.listen(options.port)
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