4

I have a websocket server written using twisted and autobahn. It is an echo server, I want to add the functionality of forwarding messages received from an multicast UDP port to the clients of the websocket server. I tried what I did for the exact same functionality in a tcp server here but that doesn't seem to work.

class SomeServerProtocol(WebSocketServerProtocol):
    def onOpen(self):
        self.factory.register(self)
        # Adding this line in TCP protocol's on connection method worked. 
        self.port = reactor.listenMulticast(6027, Listener(self), listenMultiple=True)

    def connectionLost(self, reason):
        self.factory.unregister(self)

    def onMessage(self, payload, isBinary):
        self.sendMessage(payload)

class PriceListener(DatagramProtocol):

    def __init__(self, stream):
        self.stream = stream

    def startProtocol(self):
        self.transport.setTTL(5)
        self.transport.joinGroup("0.0.0.0")

    def datagramReceived(self, datagram, address):
        # Do some processing
        # Send the data


class SomeServerFactory(WebSocketServerFactory):
    def __init__(self, *args, **kwargs):
        super(SomeServerFactory, self).__init__(*args, **kwargs)
        self.clients = {}

    def register(self, client):
        self.clients[client.peer] = {"object": client, "id": k}

    def unregister(self, client):
        self.clients.pop(client.peer)


if __name__ == "__main__":
    log.startLogging(sys.stdout)

    # static file server seving index.html as root
    root = File(".")

    factory = SomeServerFactory(u"ws://127.0.0.1:8080")
    factory.protocol = SomeServerProtocol
    resource = WebSocketResource(factory)
    # websockets resource on "/ws" path
    root.putChild(u"ws", resource)

    site = Site(root)
    reactor.listenTCP(8080, site)
    reactor.run()

I have marked in the SomeServerProtocol the line I added to have it listen on the UDP line. On removing this line everything works fine. I am getting data on UDP line, I want to push whatever data comes on the UDP line to all the clients connected to the websocket server.

I have already checked that the server works and clients are able to connect. How do I do this? Also it would be great if one could clarify why does the TCP solution wouldn't work here.

PS

I am getting the following error on the client side. WebSocket connection to 'ws://localhost:8080/ws' failed: One or more reserved bits are on: reserved1 = 1, reserved2 = 0, reserved3 = 1

I understood something about this issue here. So is the receiving of data from multicast inside the websocket protocol causing this?

0 Answers0