State in WebSocketClientProtocol

#1

Hi

I’d like to put some state information into the construction of my WebSocketClientProtocol. But haven’t found any examples of how I might do this.

Here is some code to illustrate what I would like to do.

class MyClientProtocol(WebSocketClientProtocol):

def init(self, foo):

self.foo = foo

super(MyClientProtocol, self).init()

def onOpen(self):

self.sendMessage(self.foo)

def onMessage(self, msg, binary):

print "Got echo: " + msg

def main(foo):

factory = WebSocketClientFactory(“ws://localhost:9000”)

factory.protocol = MyClientProtocol

I know what foo is here. How Can I give this to this instance on MyClientProtocol ???

connectWS(factory)

try:

reactor.run(installSignalHandlers=0)

except:

pass

I suspect that I need to define my own factory and override the function that instantiates the protocol.

Any help would be really appreciated.

Thanks!

John

0 Likes

#2

John,

I'd like to put some state information into the construction of my
WebSocketClientProtocol. But haven't found any examples of how I might
do this.

Actually, there is an example:

https://github.com/tavendo/AutobahnPython/blob/master/examples/websocket/echo/client_with_params.py

Hope that helps,
Tobias

0 Likes