Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from twisted.internet import reactor
- from autobahn.websocket import WebSocketServerFactory, WebSocketServerProtocol
- from threading import Thread
- class KeySocketProtocol(WebSocketServerProtocol):
- def __init__(self, factory):
- self.factory = factory
- def onOpen(self):
- self.factory.clients.append(self)
- def onClose(self):
- self.factory.clients.remove(self)
- class KeySocketFactory(WebSocketServerFactory):
- debug = False
- def __init__(self):
- self.clients = []
- def buildProtocol(self, addr):
- return KeySocketProtocol(self)
- class KeySocketServer(Thread):
- def __init__(self):
- self.factory = KeySocketFactory()
- self.factory.protocol = KeySocketProtocol
- reactor.listenTCP(1337, self.factory)
- Thread.__init__(self)
- def run(self):
- reactor.run(installSignalHandlers=0)
- def sendKey(self, key):
- for client in self.factory.clients:
- client.sendMessage(key)
Add Comment
Please, Sign In to add comment