XMPPFramework-创建一个XMPPRoom [英] XMPPFramework - Create an XMPPRoom
问题描述
我一直在尝试使用下面提到的代码创建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()];
}
您是否在要监视XMPPRoom委托的视图控制器的.h文件中添加了<XMPPRoomDelegate>
协议?
应该是这样的:@interface YourViewController : UIViewController <..., XMPPRoomDelegate>
.
当然#import "XMPPRoom.h"
也应该在提到的视图控制器的.h文件中.
添加:
您必须设置XMPPStream
对象,使用XMPPJID
连接到聊天"服务器(在大多数情况下为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];
}
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()];
}
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];
}
这篇关于XMPPFramework-创建一个XMPPRoom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!