How to init twisted reactor in the right way?

995 Views Asked by At

i have a class MyJabber which init a basic jabber account that print the incoming messages to stdout + put them into a queue.

The code that add the client to the reactor is this:

def addReactor(self):
    print 'inside  AddReactor'
    factory = client.basicClientFactory(self.jid, self.option['jabber']['password'])
    print "factory initialized"
    factory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self.authd)
    print 'factory bootsraped'
    reactor.connectTCP(self.option['jabber']['server'], 5222, factory)

it's called in this way:

jabber = MyJabber(options, to_irc)
jabber.addReactor()
reactor.run()

When i launch the app i see the 'print' of addReactor but after that nothing anymore. i see via 'tcpdump' that something is trying to connect to 'jabber.org' but nothing related to the authd def:

def authd(self, xmlstream):
    global thexmlstream
    thexmlstream = xmlstream
    # need to send presence so clients know we're
    # actually online.
    print 'Initializing...'
    presence = domish.Element(('jabber:client', 'presence'))
    presence.addElement('status').addContent('Online')

    xmlstream.send(presence)
    # add a callback for the messages
    print 'Add gotMessaged callback'
    xmlstream.addObserver('/message', gotMessage)
    print 'Add * callback'
    xmlstream.addObserver('/*', gotSomething)
2

There are 2 best solutions below

2
On

This doesn't seem to really be a question about how to "init twisted reactor". Rather, it seems to be more about how to use Twisted Words' XMPP support to send and respond to XMPP messages.

You can find a couple examples which do this in the Twisted Words examples directory:

http://twistedmatrix.com/documents/current/words/examples/

Try xmpp_client.py and jabber_client.py.

0
On

Fixed, there were 2 errors.

1) I accidentally forgot that a JID is [email protected]/extra

2) Forgot to add self. to gotMessage/gotSomething

I've also made addReactor return the factory and in the main() wrote:

jabber = MyJabber(options, to_irc)
factory = jabber.addReactor()
reactor.connectTCP(options['jabber']['server'], 5222, factory)
reactor.run()