0

I have two protocols, one a WebSocket server the other a ZeroMQ pull socket. I want to forward things received my ZMQ to the WebSocket. Is this possible?

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()
nickponline
  • 25,354
  • 32
  • 99
  • 167
  • Do you want to broadcast the 0MQ event to every WebSocket client or to a specific one? If the latter, how is the "specific" one identified? – oberstet Mar 12 '14 at 00:12

1 Answers1

0

Have a look at this example. New WebSocketServerProtocol instances register themselfes on the WebSocketServerFactory. The latter then has a broadcast method to dispatch an event on all currently connected clients.

Given that, all you need is put a reference to the WebSocketServerFactory onto your MyProto instance (s.factory_ws = factory_ws) and then call the broadcast method from your 0MQ proto.

oberstet
  • 21,353
  • 10
  • 64
  • 97