Pregunta

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)

And here's my socket class:

class BridgeSocket(WebSocketClientProtocol):
    def __init__(self,producer_queue):
        self.producer_queue = producer_queue

    def sendHello(self):
        self.sendMessage("hello")

    def onOpen(self):
        self.sendHello()
.....

However I am getting error

exceptions.AttributeError: BridgeSocket instance has no __call__ method

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

¿Fue útil?

Solución

One option is to do

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)

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

Otros consejos

In case someone needs it,I also tried this approach which works but oberstet's solution looks a lot elegant and efficient.

class BridgeSocket(WebSocketClientProtocol):

    def __init__(self,factory,job_queue):
        self.job_queue = job_queue
        self.factory=factory

class BridgeSocketClientFactory(WebSocketClientFactory):

    def __init__(self,url,job_queue):
        WebSocketClientFactory.__init__(self,url)
        self.job_queue = job_queue

    def buildProtocol(self, addr):
        return BridgeSocket(self,self.job_queue)


factory = BridgeSocketClientFactory("ws://localhost:7096",job_queue)
connectWS(factory)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top