覆盖 Autobahn/Twisted WebsocketClientProtocol 类 [英] Override Autobahn/Twisted WebsocketClientProtocol class

查看:38
本文介绍了覆盖 Autobahn/Twisted WebsocketClientProtocol 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想覆盖我的 WebSocketClientFactory 类以允许传入数据填充作业队列.这是我正在尝试的连接代码

I want to override my WebSocketClientFactory class to allow a job queue to be filled by the incoming data. Here's the connection code I am trying

    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 之间共享队列.

Is there any way I can share queues between my main threads and the websockets I create within them.

推荐答案

一种选择是做

factory = WebSocketClientFactory("ws://localhost:7096")
factory.job_queue = Queue.Queue()
factory.protocol = BridgeSocket

然后像这样从你的协议中访问共享队列

and then access the shared queue from within your protocol like this

class BridgeSocket(WebSocketClientProtocol):

    def onMessage(self, payload, isBinary):
        self.factory.job_queue.put(payload)

*旁注:您使用的是来自 GitHub 的 AutobahnPython trunk 吗?您应该使用 PyPI (0.6.5) 中的标记版本或最新版本.*

*Sidenote: Are you using AutobahnPython trunk from GitHub? You should be using a tagged version or the latest from PyPI (0.6.5).*

这篇关于覆盖 Autobahn/Twisted WebsocketClientProtocol 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆