<s> import json <EOL> from django . core . management import BaseCommand <EOL> from tornado import web , ioloop , gen <EOL> import tornadoredis <EOL> from sockjs . tornado import SockJSRouter , SockJSConnection <EOL> from django . conf import settings <EOL> class SubscriptionsConnection ( SockJSConnection ) : <EOL> """<STR_LIT>""" <EOL> def _create_redis ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = tornadoredis . Client ( ) <EOL> self . client . connect ( ) <EOL> @ gen . engine <EOL> def listen ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _create_redis ( ) <EOL> yield gen . Task ( self . client . subscribe , settings . REDIS_PUSH ) <EOL> self . client . listen ( self . on_redis ) <EOL> def on_message ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> data = json . loads ( msg ) <EOL> if data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> self . owner = data [ '<STR_LIT>' ] <EOL> self . listen ( ) <EOL> def on_redis ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> if msg . kind == '<STR_LIT:message>' : <EOL> data = json . loads ( msg . body ) <EOL> if data [ '<STR_LIT>' ] == self . owner or data [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> self . send ( json . dumps ( data ) ) <EOL> class Command ( BaseCommand ) : <EOL> help = '<STR_LIT>' <EOL> def handle ( self , port = <NUM_LIT> , * args , ** kwargs ) : <EOL> router = SockJSRouter ( SubscriptionsConnection , '<STR_LIT>' ) <EOL> app = web . Application ( router . urls ) <EOL> app . listen ( port ) <EOL> ioloop . IOLoop . instance ( ) . start ( ) </s>
