Skip to content

Instantly share code, notes, and snippets.

@mishudark
Created November 29, 2010 23:54
Show Gist options
  • Save mishudark/720859 to your computer and use it in GitHub Desktop.
Save mishudark/720859 to your computer and use it in GitHub Desktop.
pequeña demo de websockets en python con Tornado web... bastante incompleta por cierto
import logging
import tornado.auth
import tornado.escape
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import os.path
import uuid
from tornado import websocket
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
websockets = []
class webapp(websocket.WebSocketHandler):
def open(self):
print "WebSocket opened"
if self not in websockets:
websockets.append(self)
def on_message(self, message):
self.write_message("Me: " + message)
print "WebSocket message="+message
for websock in websockets:
if websock != self:
websock.write_message("other:" + message)
def on_close(self):
print "WebSocket closed"
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", webapp),
]
settings = dict(
cookie_secret="43oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
xsrf_cookies=True,
)
tornado.web.Application.__init__(self, handlers, **settings)
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