Created
September 22, 2012 08:54
-
-
Save chrisallick/3765584 to your computer and use it in GitHub Desktop.
main_wss.py
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 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