ios - 如何使用 iOS XMPP Robbie Hanson 库获取具有成员的 MUC 房间的成员列表

标签 ios objective-c xmpp openfire muc

我正在使用 git 上可用的 Robby Hanson 的 XMPP 库,并且我正在尝试实现 MUC 或群聊室。

我正在使用一个用户创建房间,然后尝试加入,而不邀请另一个用户。问题是,如果我尝试连接另一个用户,而不是房间的创建者,我会收到错误:

<iq xmlns="jabber:client" type="error" id="A7F05488-4A84-4EC0-8A6C-0F1541690534" from="newroom4@conference.administrator" to="newuser229@administrator/abdbd1bc"><query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="member"/></query><error code="403" type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>

我还搜索了错误,发现如果用户被禁止,可能会出现错误 403。这不是这里的情况。
因此,当我尝试获取诸如 fetchConfigurationForm 或 fetchMembersList 之类的房间信息时会发生错误。

所以,这是我正在使用的代码:
- (void)testGroupButtonFunction{
    XMPPRoomMemoryStorage *roomStorage = [[XMPPRoomMemoryStorage alloc] init];
    XMPPJID *roomJID = [XMPPJID jidWithString:@"newRoom4@conference.administrator"];

    xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomStorage
                                                 jid:roomJID                                                    
                                    dispatchQueue:dispatch_get_main_queue()];
    [xmppRoom activate:[self appDelegate].xmppStream];
    [xmppRoom addDelegate:self
            delegateQueue:dispatch_get_main_queue()];
    [xmppRoom joinRoomUsingNickname:[self appDelegate].xmppStream.myJID.user
                            history:nil
                           password:nil];
}

- (void)handleDidJoinRoom:(XMPPRoom *)room withNickname:(NSString *)nickname{

    NSLog(@"handleDidJoinRoom");

}

- (void)handleIncomingMessage:(XMPPMessage *)message room:(XMPPRoom *)room{

    NSLog(@"Incomming message: %@", message.debugDescription);

}

- (void)handleOutgoingMessage:(XMPPMessage *)message room:(XMPPRoom *)room{

    NSLog(@"Outgoing message: %@", message.debugDescription);

}

- (void)xmppRoom:(XMPPRoom *)sender didFetchMembersList:(NSArray *)items{

    NSLog(@"didFetchMembersList: %@", items.debugDescription);

}

- (void)xmppRoom:(XMPPRoom *)sender didNotFetchMembersList:(XMPPIQ *)iqError{

    NSLog(@"didNotFetchMembersList error: %@", iqError.debugDescription);

}

- (void)xmppRoomDidCreate:(XMPPRoom *)sender{

    NSLog(@"xmppRoomDidCreate");

}

- (void)xmppRoom:(XMPPRoom *)sender didConfigure:(XMPPIQ *)iqResult{

    NSLog(@"didConfigure: %@", iqResult.debugDescription);

}

- (void)xmppRoomDidJoin:(XMPPRoom *)sender {

    NSLog(@"xmppRoomDidJoin");
// I use the same code to create or join a room that's why I commented the next line
//    [xmppRoom fetchConfigurationForm];
    //Next line generates the error:
    [xmppRoom fetchMembersList];

}

- (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm{

    NSLog(@"didFetchConfigurationForm");

    NSXMLElement *newConfig = [configForm copy];
    NSArray *fields = [newConfig elementsForName:@"field"];
    for (NSXMLElement *field in fields)
    {
        NSString *var = [field attributeStringValueForName:@"var"];
        NSLog(@"didFetchConfigurationForm: %@", var);
        // Make Room Persistent
        if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) {
            [field removeChildAtIndex:0];
            [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
        }
        if ([var isEqualToString:@"muc#roomconfig_roomdesc"]) {
            [field removeChildAtIndex:0];
            [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"Apple"]];
        }
    }

    [sender configureRoomUsingOptions:newConfig];

}

- (void)xmppRoom:(XMPPRoom *)sender didNotConfigure:(XMPPIQ *)iqResult{

    NSLog(@"didNotConfigure: %@",iqResult.debugDescription);

}

我使用相同的代码来创建或加入房间,这就是我评论下一行的原因:
[xmppRoom fetchConfigurationForm];
另外我想补充一下我设置的:

公共(public)房间:1
主持:0
仅限成员(member):0
可邀请:1
房间密码:无
可以注册:1
canDiscoverJID : 1
日志启用:1

此外,如果我尝试从一个设备发送消息,当我在与另一个用户(该用户不是组的创建者/管理员)记录的第二个设备上检索消息时,我会使用 LOG_LEVEL_VERBOSE 在控制台中看到传入消息,但它不调用委托(delegate)方法。
知道为什么不调用委托(delegate)方法吗? (我确实在 .h 中添加了 XMPPRoomDelegate)
谁能帮我解决这个错误?
非常感谢您的耐心和支持!

最佳答案

是因为它没有遵循 XEP0045 指令。你应该做一个实现这个方法的类别:

- (void)joinRoomByContact:(Contact *)contact history:(NSXMLElement *)history
{
    dispatch_block_t block = ^{ @autoreleasepool {

        // Check state and update variables

        // <presence to='darkcave@chat.shakespeare.lit/firstwitch'>
        //   <x xmlns='http://jabber.org/protocol/muc'/>
        //     <history/>
        //     <password>passwd</password>
        //   </x>
        // </presence>

        NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:XMPPMUCNamespace];
        if (history)
        {
            [x addChild:history];
        }

        //XMPPPresence *presence = [XMPPPresence presenceWithType:nil to:myRoomJID];
        XMPPElement * presence = [[XMPPElement alloc] initWithName:@"presence"];
        [presence addAttributeWithName:@"to" stringValue:[NSString stringWithFormat:@"%@/%@",roomJID.bare,contact.name]];
        [presence addAttributeWithName:@"from" stringValue:contact.jid];
        [presence addChild:x];


        [xmppStream sendElement:presence];


        state |= (1 << 3);

    }};

    if (dispatch_get_specific(moduleQueueTag))
        block();
    else
        dispatch_async(moduleQueue, block);
}

我希望对你有帮助;)

关于ios - 如何使用 iOS XMPP Robbie Hanson 库获取具有成员的 MUC 房间的成员列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29683442/

相关文章:

iphone - 在屏幕上随机移动一个对象

node.js - 如何使用 XMPP 服务器发送推送通知?

java - Android 中的 XMPP 连接错误

java - XMPP 与 Android Studio 上的 Smack 4.1 连接

ios - Crashlytics 中无崩溃 session 的百分比是错误的

ios - 从 UIWebView 中的链接打开 iOS6 Apple Maps 应用程序

ios - 振动和警报在 iOS 中不起作用

objective-c - 如何根据对象字符串属性在 Xcode 中设置条件断点?

ios - iphone 应用程序中单个 viewController 的横向方向

ios - 如何在 ios 中以编程方式使用自定义字体样式