-
-
Save pyzen/2c6dcba8e85a5f00d715 to your computer and use it in GitHub Desktop.
Revisions
-
mrjoes revised this gist
Jun 21, 2013 . 2 changed files with 13 additions and 20 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,3 @@ Tornado>=2.2 sockjs-tornado toredis This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,19 +4,17 @@ import tornado.ioloop import tornado.web import sockjs.tornado import toredis # Our sockjs connection class. # sockjs-tornado will create new instance for every connected client. class BrokerConnection(sockjs.tornado.SockJSConnection): clients = set() def on_open(self, info): logging.info('Incoming client from %s' % info.ip) self.clients.add(self) def on_message(self, message): @@ -26,11 +24,12 @@ def on_close(self): self.clients.remove(self) @classmethod def pubsub(cls, data): msg_type, msg_chan, msg = data if msg_type == 'message': logging.debug('Pushing: %s' % msg) for c in cls.clients: c.send(msg) if __name__ == "__main__": @@ -58,22 +57,16 @@ def pubsub(cls, msg): v = parser.parse_args() # Initialize tornado-redis and subscribe to key rclient = toredis.Client() rclient.connect(v.redis_server, v.redis_port) rclient.subscribe(v.key, BrokerConnection.pubsub) # Initialize sockjs-tornado and start IOLoop BrokerRouter = sockjs.tornado.SockJSRouter(BrokerConnection, v.endpoint) app = tornado.web.Application(BrokerRouter.urls) app.listen(v.port) logging.info('Listening on port %d for redis key %s', v.port, v.key) tornado.ioloop.IOLoop.instance().start() -
mrjoes revised this gist
Aug 7, 2012 . No changes.There are no files selected for viewing
-
mrjoes created this gist
Aug 7, 2012 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ 1. pip install -r reqs.pip 2. server.py 3. open client.html in browser 4. redis-cli publish push '123456' 5. check browser console This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ <!DOCTYPE html> <html> <body> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script> <script> $(function() { var conn = new SockJS('http://localhost:8080/push'); conn.onmessage = function(e) { console.log('Got', e.data); } }); </script> </body> </html> This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ Tornado>=2.2 sockjs-tornado tornado-redis This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,79 @@ # -*- coding: utf-8 -*- import sys import argparse import tornado.ioloop import tornado.web import sockjs.tornado import tornadoredis # Our sockjs connection class. class BrokerConnection(sockjs.tornado.SockJSConnection): clients = set() def on_open(self, info): logging.info('Incoming client from %s' % info.ip) self.clients.add(self) def on_message(self, message): logging.debug('Received something from client: %s', message) def on_close(self): self.clients.remove(self) @classmethod def pubsub(cls, msg): if msg.kind == 'message': logging.debug('Pushing: %s' % msg.body) for c in cls.clients: c.send(msg.body) if __name__ == "__main__": # Logging import logging logging.getLogger().setLevel(logging.DEBUG) # Parse options. TODO: Use decent option parsing library. parser = argparse.ArgumentParser() parser.add_argument('--endpoint', default='/push', dest='endpoint', help='SockJS URL endpoint') parser.add_argument('--port', type=int, default=8080, dest='port', help='SockJS server port') parser.add_argument('--key', default='push', dest='key', help='Redis key') parser.add_argument('--redis_server', default='localhost', dest='redis_server', help='Redis host') parser.add_argument('--redis_port', default=6379, dest='redis_port', help='Redis port') v = parser.parse_args() # Initialize tornado-redis and subscribe to key rclient = tornadoredis.Client(host=v.redis_server, port=v.redis_port) rclient.connect() rclient.subscribe(v.key, lambda s: rclient.listen(BrokerConnection.pubsub)) # Initialize sockjs-tornado and start IOLoop BrokerRouter = sockjs.tornado.SockJSRouter(BrokerConnection, v.endpoint) app = tornado.web.Application(BrokerRouter.urls) app.listen(v.port) logging.info('Listening on port %d for redis key %s', v.port, v.key) tornado.ioloop.IOLoop.instance().start()