繁体   English   中英

XMPPFramework - 实施群聊(MUC)

[英]XMPPFramework - Implement Group Chat (MUC)

我正在使用iOS聊天客户端。 有人可以帮我多用户聊天吗?

我已经实现了Robbiehanson的XMPPFramework。

谁能告诉我如何获取组列表并使用此框架在服务器中创建组?

提前致谢。

获取房间列表:

NSString* server = @"chat.shakespeare.lit"; //or whatever the server address for muc is
XMPPJID *servrJID = [XMPPJID jidWithString:server];
XMPPIQ *iq = [XMPPIQ iqWithType:@"get" to:servJID];
[iq addAttributeWithName:@"from" stringValue:[xmppStream myJID].full];
NSXMLElement *query = [NSXMLElement elementWithName:@"query"];
[query addAttributeWithName:@"xmlns" stringValue:@"http://jabber.org/protocol/disco#items"];
[iq addChild:query];
[xmppStream sendElement:iq];

检查委托方法中的响应:

- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq{
    DDLogVerbose(@"%@", [iq description]);
    return NO;
}

加入或创造空间

XMPPRoomMemoryStorage * _roomMemory = [[XMPPRoomMemoryStorage alloc]init];
NSString* roomID = @"roomExample@chat.shakespeare.lit";
XMPPJID * roomJID = [XMPPJID jidWithString:roomID];
XMPPRoom* xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:_roomMemory
                                             jid:roomJID
                                   dispatchQueue:dispatch_get_main_queue()];
[xmppRoom activate:self.xmppStream];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoom joinRoomUsingNickname:@"myNickname"
                        history:nil
                       password:nil];

检查XMPPRoom委托方法中的响应:

- (void)xmppRoomDidCreate:(XMPPRoom *)sender{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}

- (void)xmppRoomDidJoin:(XMPPRoom *)sender{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}

更新

配置房间:

后:

[xmppRoom joinRoomUsingNickname:self.xmppStream.myJID.user
                        history:history
                       password:nil];

加:

[xmppRoom fetchConfigurationForm];

并检查响应:

- (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm{
    DDLogVerbose(@"%@: %@ -> %@", THIS_FILE, THIS_METHOD, sender.roomJID.user);
}

查看configForm对象,并根据需要进行更改,然后使用[sender configureRoomUsingOptions:newConfig];

示例:要更改配置以使房间保持不变,您可以添加以下内容:

NSXMLElement *newConfig = [configForm copy];
NSArray* fields = [newConfig elementsForName:@"field"];
for (NSXMLElement *field in fields) {
    NSString *var = [field attributeStringValueForName:@"var"];
    if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) {
        [field removeChildAtIndex:0];
        [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
    }
}
[sender configureRoomUsingOptions:newConfig];

(我不熟悉NSXMLElement,所以也许有更好的方法来改变这个值)

这是一个Swift版本:

func joinRoom(with jidString: String, delegate: XMPPRoomDelegate) -> XMPPRoom {

    let roomJID = XMPPJID(string: jidString)
    let roomStorage = XMPPRoomCoreDataStorage.sharedInstance()

    let room = XMPPRoom(roomStorage: roomStorage, jid: roomJID, dispatchQueue: DispatchQueue.main)!

        room.activate(xmppStream)

        room.addDelegate(delegate, delegateQueue: DispatchQueue.main)

        // If the room is not existing, server will create one.
        room.join(usingNickname: xmppStream.myJID.user, history: nil)

        return room
    }

    // MUCRoomDelegate
    public func xmppRoomDidCreate(_ sender: XMPPRoom!) {
        print("xmppRoomDidCreate")

        // I prefer configure right after created
        sender.fetchConfigurationForm()
    }

    public func xmppRoomDidJoin(_ sender: XMPPRoom!) {
        print("xmppRoomDidJoin")
    }

    public func xmppRoom(_ sender: XMPPRoom!, didFetchConfigurationForm configForm: DDXMLElement!) {
        print("didFetchConfigurationForm")

        let newForm = configForm.copy() as! DDXMLElement

        for field in newForm.elements(forName: "field") {

            if let _var = field.attributeStringValue(forName: "var") {

                switch _var {
                case "muc#roomconfig_persistentroom":
                    field.remove(forName: "value")
                    field.addChild(DDXMLElement(name: "value", numberValue: 1))

                case "muc#roomconfig_membersonly":
                    field.remove(forName: "value")
                    field.addChild(DDXMLElement(name: "value", numberValue: 1))

                // other configures
                default:
                    break
                }

            }

        }

        sender.configureRoom(usingOptions: newForm)
    }

    public func xmppRoom(_ sender: XMPPRoom!, didConfigure iqResult: XMPPIQ!) {
        print("didConfigure")
    }
+(void)getGroupRooms{
    NSError *error = nil;
    NSXMLElement *query = [[NSXMLElement alloc] initWithXMLString:@"<query xmlns='http://jabber.org/protocol/disco#items'/>" error:&error];
    XMPPIQ *iq = [XMPPIQ iqWithType:@"get" to:[XMPPJID jidWithString:Jabber_groupChat_Domain_server] elementID:[[[PXMPPManager sharedInstance] xmppStream] generateUUID] child:query];
    [iq addAttributeWithName:@"from" stringValue:[[[PXMPPManager sharedInstance] xmppStream] myJID].full];
    [[[PXMPPManager sharedInstance] xmppStream] sendElement:iq];

//<iq type="get" 
//to="conference.cnr-uat.panamaxil.com" 
//id="DF27F28E-488D-4DAB-AA03-399A4CDE91B3" 
//from="919414184320@cnr-uat.panamaxil.com/iphone">
//<query xmlns="http://jabber.org/protocol/disco#items"/>
//</iq>
}

#pragma - mark XMPPStreamDelegate Methods

- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq{
//    NSLog(@"Did receive IQ");

    if([iq isResultIQ])
    {
        if([iq elementForName:@"query" xmlns:@"http://jabber.org/protocol/disco#items"])
        {
            NSLog(@"Jabber Server's Capabilities: %@", [iq XMLString]);

            NSXMLElement *queryElement = [iq elementForName:@"query" xmlns:@"http://jabber.org/protocol/disco#items"];
            NSArray *items = [queryElement elementsForName:@"item"];
            NSMutableArray *arrGroupName = [[NSMutableArray alloc] init];
            for (NSXMLElement *i in items) {
                NSString *roomName = [i attributeStringValueForName:@"name"];
                NSString *jidString = [i attributeStringValueForName:@"jid"];
                //XMPPJID *jid = [XMPPJID jidWithString:jidString];

                NSDictionary *dict = @{
                                       @"groupName" : roomName,
                                       @"groupJID" : jidString,
                                       };
                [arrGroupName addObject:dict];
            }

            [ConversationsModel saveGroupName:arrGroupName];
        }
    }

    return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM