我有两个协议,一个是WebSocket服务器,另一个是ZeroMQ拉取插槽。我想把收到ZMQ的东西转发给WebSocket。这可能吗?
class WebSocketProtocol(WebSocketServerProtocol):
def onMessage(self, msg, binary):
print "Received: ", msg, binary
self.sendMessage(msg, binary)
class MyProto(ZmqPullConnection):
def onPull(self, message):
print "Recevied: ", message
# How to I call sendMessage(message) from here?
if __name__ == '__main__':
zf = ZmqFactory()
e = ZmqEndpoint("bind", "tcp://127.0.0.1:5500")
s = MyProto(zf, e)
factory_ws = WebSocketServerFactory("ws://127.0.0.1:9500/echo", debug = False)
factory_ws.protocol = WebSocketProtocol
listenWS(factory_ws)
reactor.run()
答案 0 :(得分:0)
查看this示例。新WebSocketServerProtocol
个实例在WebSocketServerFactory
上注册自己。然后,后者使用broadcast
方法在所有当前连接的客户端上发送事件。
鉴于此,您只需要在WebSocketServerFactory
实例(MyProto
)上引用s.factory_ws = factory_ws
,然后从您的0MQ原型中调用broadcast
方法。