pyxmpp: quick tutorial for creating a muc client?

只愿长相守 提交于 2020-01-12 08:00:34

问题


I'm attempting to write a quick load-test script for our ejabberd cluster that simply logs into a chat room, posts a couple of random messages, then exits.

We had attempted this particular test with tsung, but according to the authors, the muc functionality did not make it into this release.

pyxmpp seems to have this functionality, but darned if I can figure out how to make it work. Here's hoping someone has a quick explanation of how to build the client and join/post to the muc.

Thanks!


回答1:


Hey I stumbled over your question a few times, while trying the same thing. Here is my answer:

Using http://pyxmpp.jajcus.net/svn/pyxmpp/trunk/examples/echobot.py as a quickstart, all you have to do is import the MUC-Stuff

from pyxmpp.jabber.muc import MucRoomState, MucRoomManager

And once your Client is connected, you can connect to your room:

def session_started(self): 
    """Handle session started event. May be overriden in derived classes. 
    This one requests the user's roster and sends the initial presence.""" 
    print u'SESSION STARTED'
    self.request_roster() 
    p=Presence() 
    self.stream.send(p) 
    print u'ConnectToParty'
    self.connectToMUC()

def connectToMUC(self):
    self.roomManager = MucRoomManager(self.stream);
    self.roomHandler = MucRoomHandler()
    self.roomState = self.roomManager.join(
        room=JID('room@conference.server.domain'),
        nick='PartyBot',
        handler=self.roomHandler, 
        history_maxchars=0,
        password = None)
    self.roomManager.set_handlers()

To send a message, all you have to do is call self.roomState.send_message("Sending this Message")

To do stuff, inherit from MucRoomHandler and react on events. Notice the "set_handlers()" to roomManager though, it's is important, otherwise callbacks will not be called..



来源:https://stackoverflow.com/questions/2364039/pyxmpp-quick-tutorial-for-creating-a-muc-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!