XMPPFramework - 创建XMPPRoom

时间:2013-12-24 19:02:25

标签: ios objective-c xmpp xmppframework

我一直在尝试使用下面提到的代码创建一个XMPPRoom,我已经在线查看了各种示例,但是当我使用此代码时,委托xmppRoomDidCreate或xmppRoomDidJoin委托不会被调用。我不确定我在这里做错了什么?

PS:xmppStream的代表被调用,它被连接并授权但问题是XMPPRoom委托......

- (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()];
}

2 个答案:

答案 0 :(得分:6)

您是否在要监视XMPPRoom代理的视图控制器的.h文件中添加了<XMPPRoomDelegate>协议?

应该是这样的:@interface YourViewController : UIViewController <..., XMPPRoomDelegate>

当然#import "XMPPRoom.h"也应该在上述视图控制器的.h文件中。

此外:

您必须设置XMPPStream对象,使用XMPPJID连接到您的'chat'服务器(在大多数情况下为Jabber服务器),然后侦听服务器响应,然后使用密码进行身份验证然后启动如果从上面提到的所有内容都顺利进行,则会XMPPRoom创建。

示例:

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

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

然后连接到服务器:

[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]);
}

侦听服务器响应(不要忘记在.h文件中添加<XMPPStreamDelegate>):

- (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]);
    }
}

侦听服务器响应以进行身份​​验证状态:

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

然后尝试通过在验证成功后调用函数来创建XMPPRoom:

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

答案 1 :(得分:3)

这是适合我的解决方案。您必须实施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];
}