Python-twisted: trying to get UDP and websockets to work together?

I have a websocket server written using twistedand autobahn. This is an echo server, I want to add the functionality of forwarding messages received from a multicast UDP port to clients of websocket server clients. I tried what I did for the same functions on the tcp server, but this does not seem to work.

class SomeServerProtocol(WebSocketServerProtocol):
    def onOpen(self):
        self.factory.register(self)
        # Adding this line in TCP protocol 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 marked the SomeServerProtocolline that I added to listen to it over the UDP line. When you delete this line, everything works fine. I receive data over the UDP line, I want all the data to come over the UDP line to all clients connected to the websocket server.

, , . ? , , TCP .

PS

. WebSocket connection to 'ws://localhost:8080/ws' failed: One or more reserved bits are on: reserved1 = 1, reserved2 = 0, reserved3 = 1

- . websocket, ?

+4

Source: https://habr.com/ru/post/1687272/


All Articles