XMPPFramework – 创build一个XMPPRoom

我一直在尝试使用下面提到的代码创build一个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对象,使用XMPPStream连接到您的“聊天”服务器(大多数情况下为Jabber服务器),然后侦听服务器响应,然后使用密码进行身份validation,然后从上面提到的所有事情都顺利地开始创buildXMPPRoom

例:

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

监听服务器对authentication状态的响应:

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

然后尝试在validation成功后通过调用函数来创buildXMPPRoom:

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

这是为我工作的解决scheme。 你必须实现XMPPRoomDelegate

 - (void)createChatRoom { XMPPRoomMemoryStorage *roomStorage = [[XMPPRoomMemoryStorage alloc] init]; /** * Remember to add 'conference' in your JID like this: * eg 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]; }