I’ve used various python websocket libs with limited success. Most work within a while loop and use a blocking recv() function. I would send a command and then recv() some data and then do things based on that data. I’m not used to working onEvent so i’m a bit unfamiliar with how to do things while in reactor.run(). Send commands to the socket server, start timers, open new connections to other sockets, etc.
I started with the below example and built from it. Once reactor.run() hits, then i’m stuck within the class. Is it ok to stuff the class with all my while loops and other code? What is the proper way to do this?
from twisted.internet import reactor from autobahn.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS class EchoClientProtocol(WebSocketClientProtocol): def sendHello(self): self.sendMessage("Hello, world!") def onOpen(self): self.sendHello() def onMessage(self, msg, binary): print "Got echo: " + msg reactor.callLater(1, self.sendHello) if __name__ == '__main__': factory = WebSocketClientFactory("ws://localhost:9000") factory.protocol = EchoClientProtocol connectWS(factory) reactor.run()