我在 Openfire 服务器和两个用户上创建了一个组。我能够使用 XMPPFramework 对两个用户进行身份验证,但无法在他们之间交换消息。我可以看到两个用户都加入了 Openfire 服务器上的组。
使用以下服务器详细信息:
#define kHostName @"Some Server URL"
#define kServerName @"Some Server Name"
#define kRoomAddress @"testchatroom@conference.Some Server Name"
使用以下代码进行流设置和创建房间:
- (void)setupStream {
NSString *jabberID = [UserDefaults stringForKey:kUserID];
NSString *myPassword = [UserDefaults stringForKey:kUserPassword];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
self.xmppStream = [[XMPPStream alloc] init];
[self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
self.xmppStream.hostName = kHostName;
self.xmppStream.hostPort = OptionPort;
NSString *userName = [NSString stringWithFormat:@"%@@%@", jabberID, kServerName];
self.xmppStream.myJID = [XMPPJID jidWithString:userName];
password = myPassword;
NSError *error = nil;
if (OptionOldSchoolSSL)
[self.xmppStream oldSchoolSecureConnectWithTimeout:timeOut error:&error];
else
[self.xmppStream connectWithTimeout:timeOut error:&error];
}
#pragma mark -
#pragma mark XMPP delegates
- (void)xmppStreamDidConnect:(XMPPStream *)sender {
isOpen = YES;
Class authClass = nil;
if ([OptionAuthenticationMethod isEqual:XMPPAuthenticationMethodPlain])
authClass = [XMPPPlainAuthentication class];
else if ([OptionAuthenticationMethod isEqual:XMPPAuthenticationMethodDigestMD5])
authClass = [XMPPDigestMD5Authentication class];
else {
NSLog(@"Unrecognized auhthentication method '%@', falling back on Plain",
OptionAuthenticationMethod);
authClass = [XMPPPlainAuthentication class];
}
id<XMPPSASLAuthentication> auth = [[authClass alloc] initWithStream:sender
password:password];
NSError *error = nil;
if (![sender authenticate:auth error:&error])
NSLog(@"Error authenticating: %@", error);
else
NSLog(@"Authenticated !!");
}
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
[self createRoom];
}
- (void)createRoom {
self.roomStorage = [[XMPPRoomMemoryStorage alloc] init];
XMPPJID *roomJID = [XMPPJID jidWithString:kRoomAddress];
self.xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:self.roomStorage
jid:roomJID
dispatchQueue:dispatch_get_main_queue()];
[self.xmppRoom activate:self.xmppStream];
[self.xmppRoom addDelegate:self
delegateQueue:dispatch_get_main_queue()];
[self.xmppRoom joinRoomUsingNickname:self.xmppStream.myJID.user
history:nil
password:nil];
}
#pragma mark XMPPRoom delegates
- (void)xmppRoomDidCreate:(XMPPRoom *)sender {
NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)xmppRoomDidJoin:(XMPPRoom *)sender {
NSLog(@"%s", __PRETTY_FUNCTION__);
NSString *jabberID = [UserDefaults stringForKey:kUserID];
NSString *userName = [NSString stringWithFormat:@"%@@%@", jabberID, kServerName];
XMPPJID *jid = [XMPPJID jidWithString:userName];
[sender inviteUser:jid withMessage:nil];
}
- (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm
{
NSXMLElement *newConfig = [configForm copy];
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"]];
}
}
[sender configureRoomUsingOptions:newConfig];
}
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
NSString *msg = [[message elementForName:@"body"] stringValue];
NSString *from = [[message attributeForName:@"from"] stringValue];
if (msg != nil && from != nil) {
[m setObject:msg forKey:@"msg"];
[m setObject:from forKey:@"sender"];
[_messageDelegate newMessageReceived:m];
}
}
使用以下代码发送消息:
if ([messageStr length] > 0) {
NSString *jabberID = [UserDefaults stringForKey:kUserID];
NSString *userName = [NSString stringWithFormat:@"%@@%@", jabberID, kServerName];
XMPPMessage * message = [[XMPPMessage alloc] init];
[message addAttributeWithName:@"body" stringValue:messageStr];
[message addAttributeWithName:@"sender" stringValue:userName];
[DELEGATE.xmppClient.xmppRoom sendMessage:message];
self.txtMessageField.text = @"";
NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
[m setObject:[messageStr substituteEmoticons] forKey:@"msg"];
[m setObject:@"you" forKey:@"sender"];
[m setObject:[NSString getCurrentTime] forKey:@"time"];
[self.messages addObject:m];
[self.messagesTableView reloadData];
}