XMPPFramework的MUC操作方法 [英] MUC How-to with XMPPFramework

查看:106
本文介绍了XMPPFramework的MUC操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用Robbie Hanson的XMPPFramework的iOS XMPP聊天应用程序.

I am developing an iOS XMPP chat app that utilizes Robbie Hanson's XMPPFramework.

最重要的功能已经实现-发送和接收消息.基本上,我已经构建了一个基本的功能聊天应用程序,当然还有一点点注意.

The most important functionalities have been implemented - sending and receiving messages. Basically, I've built a basic functional chat app already, with a little eye candy of course.

现在,我遇到的问题是关于MUC的问题.我从其他网站看到的代码表明XMPPRoom中存在方法initWithRoomName.但是,我克隆的git repo中没有此方法.那么,替代方案是什么呢?或者,如果没有,我该如何使用XMPPFramework创建房间?

Now, the problem I have is regarding MUC. The codes I saw from other websites show that there is a method initWithRoomName in XMPPRoom. However, this method is absent in the git repo I cloned. So, what is the alternative to this? Or, if there is none, how do I go about creating rooms using XMPPFramework?

谢谢.

推荐答案

下面是我如何解决自己的问题的方法.请注意,此解决方案根本不涉及XMPPRoom.首先,我创建了一种根据情况创建或进入房间的方法. (根据XMPP文档,用于创建的XML请求与您发送进入房间的请求相同;也就是说,如果您输入房间时房间尚不存在,则服务会为您创建房间)

Below is how I got my own problem solved. Note that this solution does not involve XMPPRoom at all. First, I created a method that, depending on the situation, either creates or enters a room. (Per XMPP documentation, the XML request for creating is the same is the same as the one you would send to enter a room; that is, if the room has does not exist yet when you enter it, the service will create it for you.)

我们在这里.这是创建/输入房间的方法.此方法的作用是将状态发送到您要创建/输入的房间.如果您是第一个进入会议室但尚未创建会议室的人,则您将自动成为其所有者和主持人.

Here we go. This is the method that creates/enters a room. What this method does is send a presence to the room which you intend to create/enter. If you are the first to enter a room and the room has not been created yet, you automatically become its owner and moderator.

- (void)createOrEnterRoom:(NSString *)roomName
{
//here we enter a room, or if the room does not yet exist, this method creates it
//per XMPP documentation: "If the room does not yet exist, the service SHOULD create the room"
//this method accepts an argument which is what you would baptize the room you wish created
NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
NSString *room = [roomName stringByAppendingString:@"@conference.jabber.com/iMac"];
[presence addAttributeWithName:@"to" stringValue:room];
 NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:@"http://jabber.org/protocol/muc"];
NSXMLElement *history = [NSXMLElement elementWithName:@"history"];
 [history addAttributeWithName:@"maxstanzas" stringValue:@"50"];
 [x addChild:history];
 [presence addChild:x];
 [[self xmppStream] sendElement:presence];
}

接下来,在声明了XMPPStream方法的AppDelegate中,我们通过检查服务器发送的状态代码来过滤在didReceivePresence方法中收到的XML响应.如果状态码为201,请执行宾果游戏!房间的创建进行得很好.除201以外的状态码含义不同,但让我们着眼于201.

Next, in the AppDelegate where XMPPStream methods are declared we filter the XML response we receive in the didReceivePresence method by checking the status code sent by the server. If the status code is 201, bingo! The room creation went just fine. Status codes other than 201 mean different things, but let's focus on 201 for our purpose.

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
     NSXMLElement *x = [presence elementForName:@"x" xmlns:@"http://jabber.org/protocol/muc#user"];
    for (NSXMLElement *status in [x elementsForName:@"status"])
    {
        switch ([status attributeIntValueForName:@"code"])
        {
            case 201: [self notifyRoomCreationOk:room];
        }
    }
 }

然后,我们告诉服务器我们正在创建即时"类型的房间,这意味着我们将发送一个IQ元素,告知它房间的默认值. notifyRoomCreationOk是成功创建会议室后在不同视图中调用的委托方法,毕竟我必须将会议室记录在文本文件中以使其持久化,以便下次我打开该应用程序时,可以看到之前创建的会议室.在我的notifyRoomCreationOk方法中,我具有sendDefaultRoomConfig,它基本上描述了本段第一句中所述的内容.

Then, we tell the server that what we are creating a room of the type "instant," which means that we will send an IQ element telling it room defaults. notifyRoomCreationOk is a delegate method called in a different view when the room creation succeeds, after all I have to record the room in a text file to make it persistent so that the next time I open the app the room I created before will be visible. In my notifyRoomCreationOk method, I have sendDefaultRoomConfig which, basically, describes what is stated in the first sentence of this paragraph.

-(void)sendDefaultRoomConfig:(NSString *)room
{
NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:@"jabber:x:data"];
[x addAttributeWithName:@"type" stringValue:@"submit"];
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:@"http://jabber.org/protocol/muc#owner"];
[query addChild:x];
XMPPIQ *iq = [XMPPIQ iq];
[iq addAttributeWithName:@"id" stringValue:[NSString stringWithFormat:@"inroom-cr%@", room]];
[iq addAttributeWithName:@"to" stringValue:room];
[iq addAttributeWithName:@"type" stringValue:@"set"];
[iq addChild:query];
[[self xmppStream ] sendElement:iq];
}

请确保在调用上述方法的视图上启用了XMPPStream,否则将无法使用.这里的所有都是它的.玩得开心XMPP-ing!

Make sure that you have XMPPStream enabled on the views that call the above methods, otherwise, these won't work. That's all there is to it. Have fun XMPP-ing!

这篇关于XMPPFramework的MUC操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆