XMPPFramework - 创建XMPPRoom

Dev*_*ali 1 xmpp objective-c ios 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()];
}
Run Code Online (Sandbox Code Playgroud)

Sih*_*vic 6

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

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

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

加成:

您必须设置XMPPStream对象,连接到您的"聊天"服务器(在大多数情况下是Jabber服务器)XMPPJID,然后监听服务器响应,然后使用密码进行身份验证,然后从XMPPRoom创建开始,如果上面提到的一切顺利.

例:

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

    [xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
Run Code Online (Sandbox Code Playgroud)

然后连接到服务器:

[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]);
}
Run Code Online (Sandbox Code Playgroud)

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

- (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]);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

然后尝试通过在验证成功后调用函数来创建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];
}
Run Code Online (Sandbox Code Playgroud)