Changeset 42
- Timestamp:
- 02/07/08 15:46:20 (4 years ago)
- Files:
-
- 4 modified
-
cometd.py (modified) (11 diffs)
-
tabled.tac (modified) (2 diffs)
-
web/js/jquery.comet.js (modified) (9 diffs)
-
web/js/poker-client.js (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
cometd.py
r41 r42 301 301 print "I'm delivering foo &&&& %s" % simplejson.dumps 302 302 303 print "$$$$$ delivering %s" % self.backlog 303 304 self.stream.write( 304 305 self.ctypeProps["envelope"] % ( … … 322 323 self.stream = stream.ProducerStream() 323 324 self.deliver() 325 326 class ClientSubscription(object): 327 328 329 def __init__( self, channel ): 330 self.channel = channel 331 self.sendMessageCallback = None #Make sure we set this!!! 332 333 334 def messageReceived(self, message): 335 self.sendMessage(message) 336 337 #Make sure you call this first if you override this message!!! 338 def sendMessage(self, message): 339 self.sendMessageCallback(message, self.channel) 340 341 #Make sure you call this first if you override this message!!! 342 def subscribe(self, channel): 343 return True 344 345 #when we get disconnected or unsubscribed 346 def unsubscribe(self): 347 pass 348 349 class SubscriptionException(Exception): 350 pass 324 351 325 352 class Client: … … 332 359 self.authToken = authToken 333 360 self.lastError = lastError 361 self.cSubscriptions = {} 362 363 #override this if you want different subscriptions to have different classes 364 def generateSubscription(self, channel): 365 return ClientSubscription(channel) 366 367 def subscribe(self, channel): 368 if channel in self.cSubscriptions: 369 raise SubscriptionException("Already subscribed") 370 else: 371 cs = self.generateSubscription(channel) 372 cs.sendMessageCallback = self.sendMessage 373 print "TRYING TO SUBSCRIBE TO " + channel 374 if cs.subscribe(channel): 375 self.cSubscriptions[channel] = cs 376 return True 377 else: 378 del(cs) 379 return False 380 381 #when we get disconnected or unsubscribed 382 def unSubscribe(self, channel): 383 if channel in self.cSubscriptions: 384 cs = self.clientSubscription(channel, self.sendMessage) 385 cs.unsubscribe(channel) 386 del(self.cSubscriptions[channel]) 387 else: 388 raise SubscriptionException("Not subscribed yet and trying to unsubscribe") 334 389 335 390 def setConnection(self, conn): 391 print "Setting Connection &&&&&&&&&&&&&& %s" % conn 336 392 self.connection = conn 337 393 338 394 def messageReceived(self, message, channel): 339 self.sendMessage(message,channel) 340 pass 395 if channel in self.cSubscriptions: 396 self.cSubscriptions[channel].messageReceived(message) 397 else: 398 raise SubscriptionException("Not subscribed yet and trying to send message") 341 399 342 400 def sendMessage(self, message, channel): … … 432 490 433 491 ctr = 0 434 if verbose: log.msg("messages:", type(messages), ":", simplejson.dumps(messages) )492 if verbose: log.msg("messages:", type(messages), ":", simplejson.dumps(messages), " :", messages) 435 493 aStream = None 436 494 print( messages) … … 442 500 client = None 443 501 localResp = [] 444 if "clientId" in m and m["clientId"] in self.clients: 502 if "clientId" in m and m["clientId"] in self.clients\ 503 and self.clients[m["clientId"]].connection is not None: 445 504 client = self.clients[m["clientId"]] 446 505 else: … … 465 524 466 525 chan = m["channel"] 526 print "CHAN: !!!!!!!!!!!!!!!!! %s " % chan 467 527 if chan == "/meta/handshake" and ctr == 0: 468 528 # looks like we'll need to create a Connection … … 620 680 # get the client and the channel here 621 681 # self._subscribe() 682 print "self.subscribe ^^^^^^^^^^^^^^^^" 622 683 if not self._checkClient(request, message): 623 684 # FIXME: we should probably send advice here instead of just raw failure … … 646 707 # FIXME: should we be calling client.deliver and having *that* dispatch 647 708 # down to the correct connection object? 709 print "Sending Message: with %s %s" % (self.clients, client) 648 710 client.connection.deliver(resp) 649 711 # return { "successful": True } … … 679 741 def _globbing_unsubscribe(self, client, chan): 680 742 "remove a subscription" 743 client.unsubscribe(chan) 681 744 682 745 cparts = chan.split("/")[1:] … … 700 763 if verbose: log.msg(cparts) 701 764 root = self.subscriptions 702 for part in cparts: # create parts of the topic tree that don't yet exist 703 if not part in root: 704 if verbose: 705 log.msg("creating part: ", part) 706 root[part] = { "__cometd_subscribers": None } 707 # root[part] = weakref.WeakValueDictionary() 708 root[part]["__cometd_subscribers"] = weakref.WeakValueDictionary() 709 710 root = root[part] 711 712 root["__cometd_subscribers"][client.id] = client 765 # make sure the subscription was successful 766 if client.subscribe( chan ): 767 for part in cparts: # create parts of the topic tree that don't yet exist 768 if not part in root: 769 if verbose: 770 log.msg("creating part: ", part) 771 root[part] = { "__cometd_subscribers": None } 772 # root[part] = weakref.WeakValueDictionary() 773 root[part]["__cometd_subscribers"] = weakref.WeakValueDictionary() 774 775 root = root[part] 776 777 root["__cometd_subscribers"][client.id] = client 713 778 714 779 def _globbing_route(self, request, message): … … 758 823 subs[client].connection.deliver(message) 759 824 return { "successful": True } 760 761 825 # vim:ts=4:noet: -
tabled.tac
r40 r42 85 85 clientChannels = [ r"\/poker" ] 86 86 87 class WebClient(cometd.Client,BasePokerTableHandler): 87 class PokerSubscription(cometd.ClientSubscription, BasePokerTableHandler): 88 def __init__(self, channel): 89 BasePokerTableHandler.__init__(self) 90 cometd.ClientSubscription.__init__(self, channel) 91 92 def messageReceived(self,message): 93 self.dataReceived(message) 94 95 def sendMessageViaTransport(self,data): 96 self.sendMessage(data) 97 98 class WebClient(cometd.Client): 88 99 89 def __init__(self, id=None, authSuccessful=False, authToken=None, lastError=""): 90 cometd.Client.__init__(self,id,authSuccessful,authToken,lastError) 91 print "INITIALIZING WEB CLIENT ((((" 92 BasePokerTableHandler.__init__(self) 93 94 def messageReceived(self,message,channel): 95 self.dataReceived(message) 96 97 def sendMessageViaTransport(self,data): 98 cometd.Client.sendMessage(self,data,"/poker") 100 def generateSubscription(self, channel): 101 return PokerSubscription( channel ) 102 103 def subscribe(self, channel): 104 #we only want poker channels 105 print "SUB ---------------- CHAN " + channel 106 if channel == "/poker": 107 return cometd.Client.subscribe(self,channel) 108 else: return False 99 109 100 110 … … 217 227 print "Listening on localhost:%d(tcp) %d(http)" % (port, port2) 218 228 219 229 # vim:filetype=python -
web/js/jquery.comet.js
r37 r42 18 18 this.connectionType = (this._bXD) ? 'callback-polling' : 'long-polling'; 19 19 20 this.startup = function(oReturn )20 this.startup = function(oReturn, fCallback) 21 21 { 22 22 if(this._comet._bConnected) return; 23 this.tunnelInit( );24 }; 25 26 this.tunnelInit = function( )23 this.tunnelInit(fCallback); 24 }; 25 26 this.tunnelInit = function(fCallback) 27 27 { 28 28 var msgConnect = … … 34 34 }; 35 35 36 this.openTunnel(msgConnect );37 }; 38 39 this.openTunnel = function(oMsg )36 this.openTunnel(msgConnect,fCallback); 37 }; 38 39 this.openTunnel = function(oMsg, fCallback) 40 40 { 41 41 $.comet._bPolling = true; 42 43 this._send($.comet._sUrl, oMsg, function(sReturn) 44 { 42 var fc = fCallback; 43 this._send($.comet._sUrl, [oMsg], function(sReturn) 44 { 45 if( fc ) { fc(); } 45 46 var oReturn = (typeof sReturn != "object") ? (eval('(' + sReturn + ')')) : sReturn; 46 47 $.comet._bPolling = false; … … 134 135 type: 'post', 135 136 beforeSend: function(oXhr) { oXhr.setRequestHeader('Connection', 'Keep-Alive'); }, 136 data: { message: JSON.stringify( [oMsg]) },137 data: { message: JSON.stringify(oMsg) }, 137 138 success: fCallback 138 139 }); … … 146 147 data: 147 148 { 148 message: JSON.stringify( [$.extend(oMsg,{connectionType: 'callback-polling' })])149 message: JSON.stringify($.extend(oMsg,{connectionType: 'callback-polling' })) 149 150 }, 150 151 success: fCallback … … 178 179 this._bTrigger = true; // this sends $.event.trigger(channel, data) 179 180 180 this.init = function(sUrl )181 this.init = function(sUrl,fCallback) 181 182 { 182 183 this._sUrl = (sUrl) ? sUrl : '/cometd'; … … 190 191 var oMsg = $.extend(msgHandshake, {id: this._nNextId++}); 191 192 192 this._oTransport._send(this._sUrl, oMsg, $.comet._finishInit); 193 }; 194 195 this._finishInit = function(sReturn) 193 this._oTransport._send(this._sUrl, [oMsg], function(sReturn){ 194 $.comet._finishInit(sReturn, fCallback)}); 195 }; 196 197 this._finishInit = function(sReturn, fCallback) 196 198 { 197 199 var oReturn = (typeof sReturn != "object") ? (eval('(' + sReturn + ')')[0]) : sReturn[0]; … … 211 213 $.comet._oTransport.version = $.comet.version; 212 214 $.comet.clientId = oReturn.clientId; 213 $.comet._oTransport.startup(oReturn );215 $.comet._oTransport.startup(oReturn,fCallback); 214 216 $.comet.endBatch(); 215 217 } 216 }; 217 218 this._sendMessage = function(oMsg) 218 219 }; 220 //batch mode defaults to null 221 this._sendMessage = function(oMsg, batchMode) 219 222 { 220 223 if($.comet._nBatch <= 0) … … 231 234 oMsg.id = $.comet._nNextId++; 232 235 } 233 234 $.comet._oTransport._send($.comet._sUrl, oMsg); 236 if( batchMode ) 237 $.comet._oTransport._send($.comet._sUrl, oMsg); 238 else 239 $.comet._oTransport._send($.comet._sUrl, [oMsg]); 235 240 } 236 241 else … … 244 249 this.endBatch = function() { 245 250 if(--this._nBatch == 0) 246 { 247 this._sendMessage(this._aMessageQueue); 251 { 252 if( this._aMessageQueue.length > 0 ) 253 this._sendMessage(this._aMessageQueue, true); 248 254 249 255 this._aMessageQueue = []; -
web/js/poker-client.js
r37 r42 61 61 } 62 62 //Do status generating code here 63 var player_names = [] 64 65 66 if( stat.table_info ) { 67 ti = stat.table_info; 68 player_names = ti.player_names; 69 for( i in player_names ) { 70 pn = player_names[i] 71 if( players[pn] == null ) { //if it's null let's create a new player object 72 newPlayer = baseplayer.clone(true); 73 74 players[pn] = new Player(newPlayer); 75 players[pn].id.text(pn); 76 if( pn == stat.you.id ) { 77 you = players[stat.you.id] 78 $("#you").append(you.pl) 79 } else { 80 player_status.append( newPlayer ); 81 } 63 var player_names = []; 64 65 66 if( stat.table_info ) { 67 ti = stat.table_info; 68 player_names = ti.player_names; 69 for( i in player_names ) { 70 pn = player_names[i] 71 if( players[pn] == null ) { //if it's null let's create a new player object 72 newPlayer = baseplayer.clone(true); 73 74 players[pn] = new Player(newPlayer); 75 players[pn].id.text(pn); 76 if( pn == stat.you.id ) { 77 you = players[stat.you.id]; 78 $("#you").append(you.pl); 79 } else { 80 player_status.append( newPlayer ); 82 81 } 83 players[pn].updatePublic( ti.players[pn] ); 84 } 85 } 82 } 83 players[pn].updatePublic( ti.players[pn] ); 84 } 85 } 86 86 if( stat.game_state ) { 87 87 gs = stat.game_state; … … 174 174 //$("form#connect_form").hide(); 175 175 // 176 $.comet.init("/cometd" );177 $.comet.subscribe("/poker", processStatus);176 $.comet.init("/cometd", function() { 177 $.comet.subscribe("/poker", processStatus);}) 178 178 179 179 sel = $('selector'); … … 200 200 }); 201 201 202
