Created
March 7, 2011 17:50
-
-
Save guyromm/858874 to your computer and use it in GitHub Desktop.
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
@app.route('/socket.io/websocket') | |
def socketio(): | |
s = request.environ['socketio'] | |
if s.on_connect(): | |
#print 'CONNECTED', locals() | |
#s.send({'buffer': buffer}) | |
#s.broadcast({'announcement': s.session.session_id + ' connected'}) | |
pass | |
game_id=None;game=None;cook=None;i_am='spectator';rdsub = redis.Redis('localhost') ; listener_trd = None | |
#redis sub listener | |
def listener(): | |
print 'starting listener' | |
while True: | |
#print 'listening..' | |
lres = rdsub.listen() | |
#print 'out of listen' | |
for lrs in lres: | |
lgameid = lrs['channel'].split(':')[1] | |
if lrs['type']=='subscribe': | |
break | |
print 'DECODING %s'%lrs | |
ldt = json.loads(lrs['data']) | |
if ldt['op']=='chat': | |
print 'SENDING BCASTED CHAT' | |
s.send(json.dumps({'op':'chat','user':ldt['user'],'text':ldt['text']})) | |
else: | |
raise Exception( "wtf %s"%ldt) | |
print 'HEARD %s'%lrs | |
break | |
while True: | |
messages = s.recv() | |
for msg in messages: | |
dt = json.loads(msg) | |
print 'GOT MSG %s'%dt | |
if dt['op']=='connect': | |
game_id = dt['gameid'] | |
cook = getauthcookie(dt['rawcookie']) | |
game = get_game(game_id) | |
if cook == game['p1cookie']: i_am='player1' | |
elif cook == game.p2cookie: i_am='player2' | |
pubkey = 'publish:'+game_id | |
print 'subscribing to game %s'%pubkey | |
rdsub.subscribe(pubkey) | |
print 'received connect on game %s, with auth cook %s. i am %s'%(game_id,cook,i_am) | |
if not listener_trd: | |
print 'SPAWNING LISTENER' | |
listener_trd = gevent.spawn(listener) | |
elif dt['op']=='send_chat': | |
pubkey = 'publish:'+game_id | |
pmsg = json.dumps({'op':'chat','user':i_am,'text':html(dt['text'])}) | |
print 'sending pub on %s : %s'%(pubkey,pmsg) | |
rcpts = g.redis.publish(pubkey,pmsg) | |
print '%s recipients'%rcpts | |
else: | |
raise Exception(dt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment