import sync import sync.Protocol clients = [] letters = {} # SYNCLetter - the letter protocol defined as a class class SYNCLetter( sync.Protocol ): # client connected to server def onConnect( self ): # add the new client to the client list self.clients.append(self) # update the number of users for c in self.clients: c.send('b '+ str(len(self.clients))) # client disconnected from server def onDisconnect( self ): # remove the client from the client list self.clients.remove(self) # update the number of users for c in self.clients: c.send('b '+ str(len(self.clients))) # server recieved data from client def onData (self, data): # convert message into an list # messages are space delimited msg = data.split(' ') # determine message type and fire function # do nothing if the message is malformed or disfuntional if msg[0] == 'a': #fire the info method with the message array self.info( msg ) elif msg[0] == 'c': #fire the letter method with the message array self.letter( msg ) # letter method def letter(self, msg): # store letters coordinates in letters object self.letters[string.split(msg[1],":")[0]] = msg[1] # send new letter coordinates to users for c in self.clients: c.send('c ' + msg[1] ) # info method def info(self, msg): # send all letter coordinates to user self.send('a '+string.join(self.letters.values(),'|')) sync.enable( 'central.powersdk.com', 1540 , SYNCLetter ) sync.start()