document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import Pyro.core
  2. from time import sleep
  3. from twisted.internet import reactor
  4. from twisted.python import log
  5. from twisted.web.server import Site
  6. from twisted.web.static import File
  7. from autobahn.websocket import WebSocketServerFactory, \\
  8.                                WebSocketServerProtocol, \\
  9.                                listenWS
  10. #connect to our pyro RPC service where we can grab our status and send commands
  11. cmd = Pyro.core.getProxyForURI("PYROLOC://localhost:7766/cmd")
  12.  
  13. class EchoServerProtocol(WebSocketServerProtocol):
  14.  
  15.    def onOpen(self):
  16.     counter=0
  17.     self.sendMessage(cmd.outputStatus())
  18.     self.factory.register(self)
  19.  
  20.    def connectionLost(self, reason):
  21.       WebSocketServerProtocol.connectionLost(self, reason)
  22.       self.factory.unregister(self)
  23. # parse your data from the webapp here when it sends a message 
  24.    def onMessage(self,msg,binary):
  25.      status = cmd.outputStatus()
  26. #check to see if is a length of 1.  since we send the bit we want to turn on not much parsing is needed.
  27.      if(len(msg)==1):
  28.     if(status[int(msg)]==\'1\'):
  29.        cmd.outputCmd(int(msg),0)
  30.     else:
  31.        cmd.outputCmd(int(msg),1)
  32.  
  33. class BroadcastServerFactory(WebSocketServerFactory):
  34.    def __init__(self, url, debug = False, debugCodePaths = False):
  35.       WebSocketServerFactory.__init__(self, url, debug = debug, debugCodePaths = debugCodePaths)
  36.       self.clients = []
  37.       self.tickcount = 0
  38.       self.data=cmd.outputStatus()
  39.       self.tick()
  40. #set up a tick that we will use to have the server send the status if it ever changes.
  41. #update rate set to sleep for .05 seconds. we also send out every 100 ticks just in case the message never arrived
  42.    def tick(self):
  43.     if(self.data!=cmd.outputStatus() or self.tickcount>100):
  44.        self.data=cmd.outputStatus()
  45.            self.sendData(self.data+\'-\'+str(self.tickcount))
  46.            self.tickcount=0
  47.         self.tickcount=self.tickcount + 1
  48.         reactor.callLater(.05,self.tick)
  49.  
  50.    def register(self, client):
  51.       if not client in self.clients:
  52.          print "registered client " + client.peerstr
  53.          self.clients.append(client)
  54.  
  55.    def unregister(self, client):
  56.       if client in self.clients:
  57.          print "unregistered client " + client.peerstr
  58.          self.clients.remove(client)
  59.  
  60.    def sendData(self,data):
  61. #   send data to each of the connected clients
  62.     for c in self.clients:
  63.        c.sendMessage(data)
  64.    
  65. if __name__ == \'__main__\':
  66.    ServerFactory = BroadcastServerFactory
  67.    factory = ServerFactory("ws://localhost:9000",debug = False)
  68.    factory.protocol = EchoServerProtocol
  69.    listenWS(factory)
  70.    reactor.run()
');