我想覆盖我的WebSocketClientFactory类,以允许传入数据填充作业队列。这是我正在尝试的连接代码
factory = WebSocketClientFactory("ws://localhost:7096")
job_queue = Queue.Queue()
factory.protocol = BridgeSocket(job_queue)
connectWS(factory)
这是我的套接字类:
class BridgeSocket(WebSocketClientProtocol):
def __init__(self,producer_queue):
self.producer_queue = producer_queue
def sendHello(self):
self.sendMessage("hello")
def onOpen(self):
self.sendHello()
.....
然而我收到错误
exceptions.AttributeError: BridgeSocket instance has no __call__ method
有什么方法可以在我的主线程和我在其中创建的websockets之间共享队列。
答案 0 :(得分:3)
一种选择是
factory = WebSocketClientFactory("ws://localhost:7096")
factory.job_queue = Queue.Queue()
factory.protocol = BridgeSocket
然后从协议中访问共享队列,如此
class BridgeSocket(WebSocketClientProtocol):
def onMessage(self, payload, isBinary):
self.factory.job_queue.put(payload)
*旁注:你是否在GitHub上使用AutobahnPython trunk ?您应该使用标记版本或PyPI(0.6.5)中的最新版本。*
答案 1 :(得分:1)
如果有人需要它,我也尝试过这种方法,但oberstet的解决方案看起来非常优雅和高效。
class BridgeSocket(WebSocketClientProtocol):
def __init__(self,factory,job_queue):
self.job_queue = job_queue
self.factory=factory
class BridgeSocketClientFactory(WebSocketClientFactory):
def __init__(self,url,job_queue):
WebSocketClientFactory.__init__(self,url)
self.job_queue = job_queue
def buildProtocol(self, addr):
return BridgeSocket(self,self.job_queue)
factory = BridgeSocketClientFactory("ws://localhost:7096",job_queue)
connectWS(factory)