source: ralphm-patches/pubsub_client_example.patch @ 83:255aae0cf8c5

Last change on this file since 83:255aae0cf8c5 was 56:fa1511cccfe2, checked in by Ralph Meijer <ralphm@…>, 9 years ago

First pre-release of Wokkel 0.7.0. Start of pubsub client example.

File size: 2.0 KB
  • new file doc/examples/pubsub_client_create_node.tac

    # HG changeset patch
    # Parent bbb746f7971831cb09e1539bfd3fefe862c3cc9e
    
    diff -r bbb746f79718 doc/examples/pubsub_client_create_node.tac
    - +  
     1"""
     2An XMPP Publish Subscribe publisher client as an XMPP client.
     3
     4This client logs in as C{publisher@example.org}, creates a node at
     5C{pubsub.example.org} and prints the node identifier.
     6
     7Note that some services may change the node identifier to adhere to local
     8policies. If you set L{NODE_IDENTIFIER} to C{None}, the service will create an
     9'instant node', with a unique node identifier.
     10"""
     11
     12from twisted.application import service
     13from twisted.internet import reactor, task
     14from twisted.python import log
     15from twisted.words.protocols.jabber.jid import JID
     16from wokkel.client import XMPPClient
     17from wokkel.pubsub import PubSubClient
     18
     19# Configuration parameters
     20
     21THIS_JID = JID('publisher@example.org')
     22SERVICE_JID = JID('pubsub.example.org')
     23NODE_IDENTIFIER = 'test_node'
     24SECRET = 'secret'
     25LOG_TRAFFIC = True
     26
     27class Publisher(PubSubClient):
     28
     29    count = None
     30    loopingCall = None
     31    nodeIdentifier = None
     32
     33    def connectionInitialized(self):
     34        def cb(nodeIdentifier):
     35            log.msg("The node %r was successfully created" % nodeIdentifier)
     36        d = self.createNode(SERVICE_JID, NODE_IDENTIFIER)
     37        d.addCallbacks(cb, log.err)
     38        d.addCallback(lambda _: reactor.stop())
     39
     40
     41    def nodeCreated(self, nodeIdentifier):
     42        self.nodeIdentifier = nodeIdentifier
     43        self.count = 0
     44        self.loopingCall = task.LoopingCall(self.doPublish)
     45        self.loopingCall.start(5)
     46
     47# Set up the Twisted application
     48
     49application = service.Application("Publish Subscribe publisher")
     50
     51client = XMPPClient(THIS_JID, SECRET)
     52client.logTraffic = LOG_TRAFFIC
     53client.setServiceParent(application)
     54
     55pubsubHandler = PubSubClient()
     56pubsubHandler.setHandlerParent(client)
Note: See TracBrowser for help on using the repository browser.