Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMPPFramework - Implement Group Chat (MUC)

I am working on the iOS chat client. Can anyone please help me with the Multi-User Chat?

I have implemented Robbiehanson's XMPPFramework.

Can anyone please let me know how to get list of group and create a group in server with this framework?

Thanks in advance.

like image 813
Satish Avatar asked Oct 09 '13 09:10

Satish


2 Answers

to get a list of rooms:

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];

check for response in delegate method:

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

to join or create room

XMPPRoomMemoryStorage * _roomMemory = [[XMPPRoomMemoryStorage alloc]init];
NSString* roomID = @"[email protected]";
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];

check for response in XMPPRoom delegate methods:

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

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

update

to configure a room:

after:

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

add:

[xmppRoom fetchConfigurationForm];

and check response in:

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

Review the configForm object, and change as needed, then send it with [sender configureRoomUsingOptions:newConfig];

example: to change the configuration to make the room persistent you can add something like:

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];

(i'm not familiar with NSXMLElement, so maybe there is a better way to change the value)

like image 54
Flores Robles Avatar answered Nov 10 '22 01:11

Flores Robles


Here is a Swift version:

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")
    }
like image 31
dichen Avatar answered Nov 10 '22 00:11

dichen