Created
September 17, 2011 22:01
-
-
Save borismus/1224416 to your computer and use it in GitHub Desktop.
Autobahn KeySocket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment