Advertisement
Guest User

asbjornenge

a guest
May 26th, 2010
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. # This is websockets.py
  2.  
  3. from twisted.protocols.basic import LineReceiver
  4.  
  5. class HandshakeError(ValueError): pass    
  6.  
  7. transports = [] # <- so this holds the different transports
  8.  
  9. class WebsocketsServerProtocol(LineReceiver):
  10.     def __init__(self):
  11.         self.__buffer = ''
  12.        
  13.     def connectionMade(self):
  14.         self.__buffer = ''
  15.         self.path = None
  16.         self.headers = {}
  17.         self.handshake = _handshake_state_machine()
  18.         self.handshake.next()
  19.         transports.append(self.transport) # <- append upon connection
  20.         pass
  21.    
  22.     def connectionLost(self, reason):
  23.         transports.remove(self.transport) # <- remove upon lost connection ( closing browser window etc. )
  24.  
  25.     # And sendMessage goes
  26.     def sendMessage(self, data):
  27.         for transport in transports:
  28.             transport.write('\0%s\xff' % data)
  29.  
  30. # And thats it :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement