XMPPFramework - Create an XMPPRoom

2019-01-27 07:21发布

问题:

I have been trying to create a XMPPRoom using the below mentioned code, i have looked at various examples online however when i use this code, the delegate xmppRoomDidCreate or xmppRoomDidJoin delegates doesn't get called. I'm not sure what am i doing wrong here?

PS: the delegates of xmppStream do get called, it gets connected and authorized however, the issue is XMPPRoom delegates...

- (void)createChatRoom
{
    NSString *jabberID = @"abcxyz@testservice.com";
    self.xmppStream.hostName = @"testservice.com";


    self.xmppStream = [[XMPPStream alloc]init];
    [self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];

    [self.xmppStream setMyJID:[XMPPJID jidWithString:jabberID]];

    NSError *error = nil;

    if (![self.xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[NSString stringWithFormat:@"Cannot connect to server %@",[error localizedDescription]] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];

        return;
    }

    // Configure xmppRoom
    XMPPJID *roomJID = [XMPPJID jidWithString:@"TestRoom@conference.testservice.com"];

    XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];

    XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomMemoryStorage
                                                           jid:roomJID
                                                 dispatchQueue:dispatch_get_main_queue()];

    [xmppRoom activate:self.xmppStream];
    [xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
}

回答1:

Did you add <XMPPRoomDelegate> protocol in the .h file of the view controller in which you want to monitor XMPPRoom's delegates?

It should be something like this: @interface YourViewController : UIViewController <..., XMPPRoomDelegate>.

Of course #import "XMPPRoom.h" should be also in .h file of the mentioned view controller.

ADDITION:

You have to setup XMPPStream object, connect to your 'chat' server (in most cases Jabber server) with XMPPJID, then listen for server response, then authentificate with the password and then to start with XMPPRoom creation if everything mentioned from above goes well.

Example:

- (void)setupStream
{
    NSAssert(xmppStream == nil, @"Method setupStream invoked multiple times");
    xmppStream = [[XMPPStream alloc] init];

    [xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}

Then to connect to server:

[self setupStream];

NSString *myJID = @"...";

[xmppStream setMyJID:[XMPPJID jidWithString:myJID]];

NSError *error2;
if ([xmppStream connect:&error2])
{
    NSLog(@"Connected to XMPP.");
}
else
{
    NSLog(@"Error connecting to XMPP: %@", [error2 localizedDescription]);
}

Listen for server response (do not forget to add <XMPPStreamDelegate> in .h file):

- (void)xmppStreamDidConnect:(XMPPStream *)sender
{    
    NSError *error;    
    if ([[self xmppStream] authenticateWithPassword:password error:&error])
    {
        NSLog(@"Authentificated to XMPP.");
    }
    else
    {
        NSLog(@"Error authentificating to XMPP: %@", [error localizedDescription]);
    }
}

Listen for server response for authentification status:

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
    NSLog(@"%s", __FUNCTION__);
    XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"];
    [sender sendElement:presence]; 
}

and then try to create XMPPRoom by calling function after authentification success:

- (void)createChatRoom
{    
    // Configure xmppRoom
    XMPPJID *roomJID = [XMPPJID jidWithString:@"TestRoom@conference.testservice.com"];

    XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];

    XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomMemoryStorage
                                                           jid:roomJID
                                                 dispatchQueue:dispatch_get_main_queue()];

    [xmppRoom activate:self.xmppStream];
    [xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
    [xmppRoom joinRoomUsingNickname:user history:nil];
}


回答2:

Here is the solution worked for me. You have to implement XMPPRoomDelegate

- (void)createChatRoom
{

    XMPPRoomMemoryStorage *roomStorage = [[XMPPRoomMemoryStorage alloc] init];

    /**
     * Remember to add 'conference' in your JID like this:
     * e.g. uniqueRoomJID@conference.yourserverdomain
     */

    NSString *groupName = [NSString stringWithFormat:@"%@@conference.192.168.0.101",groupNameTextField.text];
    NSLog(@"attempting to create room %@",groupName);
    XMPPJID *roomJID = [XMPPJID jidWithString:groupName];
    XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomStorage
                                                           jid:roomJID
                                                 dispatchQueue:dispatch_get_main_queue()];

    [xmppRoom activate:del.xmppStream];
    [xmppRoom addDelegate:self
            delegateQueue:dispatch_get_main_queue()];

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

    NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"groupChatArray"]];
    [array addObject:groupName];

    [[NSUserDefaults standardUserDefaults]setObject:array forKey:@"groupChatArray"];
    [[NSUserDefaults standardUserDefaults]synchronize];


}

- (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm{
    NSLog(@"didFetchConfigurationForm");
    NSXMLElement *newConfig = [configForm copy];
    NSLog(@"BEFORE Config for the room %@",newConfig);
    NSArray *fields = [newConfig elementsForName:@"field"];

    for (NSXMLElement *field in fields)
    {
        NSString *var = [field attributeStringValueForName:@"var"];
        // Make Room Persistent
        if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) {
            [field removeChildAtIndex:0];
            [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
        }
    }
    NSLog(@"AFTER Config for the room %@",newConfig);
    [sender configureRoomUsingOptions:newConfig];
}