New to Autobahn - Client Question

#1

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()
0 Likes

#2

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.

AutobahnPython uses Twisted for networking, which is an asynchronous, event driven framework.

http://twistedmatrix.com

I'd suggest you have a look at Twisted tutorials for asynch. programming .. deferreds and such or the other examples included with AutobahnPython as a start.

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?

You don't need to "stuff" everything in one class. Not sure waht you mean with "all my while loops" ... in any case, you must not create blocking code, but structure all your app as asynchronously, event driven code.

···

Am 14.02.2013 20:50, schrieb Jezzz:

0 Likes