在XMPP开发中,使用XMPPStream进行连接服务器后,验证过程中,比较常见的一个错误是
<failure xmlns=”urn:ietf:params:xml:ns:xmpp-sasl”><not-authorized/></failure>.
尤其作为初学者(笔者就是这样的),经常会因为这个问题浪费不少时间。
xmpp的使用
1、创建xmppStream
- (void)setupXmppStream
{
// 1. 实例化
_xmppStream = [[XMPPStream alloc] init];
[_xmppStream addDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
}
2、连接服务器
- (void)connect
{
// 1. 实例化XMPPStream
[self setupXmppStream];
NSString *hostName = @"sky.local";
// NSString *hostName = @"127.0.0.1";
NSString *userName = @"zhaoliu";
// 设置XMPPStream的hostName&JID
_xmppStream.hostName = hostName;
_xmppStream.myJID = [XMPPJID jidWithUser:userName domain:hostName resource:nil];
// 连接
NSError *error = nil;
if (![_xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]) {
NSLog(@"%@", error.localizedDescription);
} else {
NSLog(@"发送连接请求成功");
}
}
3、得到服务器相应后回调方法
#pragma mark - XMPPStream协议代理方法
#pragma mark 完成连接
- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
NSLog(@"success userName = %@, myJID = %@",sender.myJID.user, sender.myJID);
// 登录到服务器,将用户密码发送到服务器验证身份
NSString *password = @"123";
[_xmppStream authenticateWithPassword:password error:nil];
}
#pragma mark 断开连接
- (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error
{
NSLog(@"断开连接");
}
4、登陆信息回调方法(即:[_xmppStream authenticateWithPassword:password error:nil]利用此方法发送密码到服务器完成登陆)
#pragma mark 身份验证成功
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
NSLog(@"身份验证成功!");
}
#pragma mark 用户名或者密码错误
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error
{
NSLog(@"用户名或者密码错误 error = %@",error);
}
此时如果代码中的domain与openfire服务器中的设置不一样时,就会导致此错误,虽然我们用一些第三方客户端时一样能登陆成功
NSString *hostName = @"sky.local";
NSString *hostName = @"127.0.0.1";
当我用adium、spark等第三方软件时,上面的两个都可以登陆成功。但是用代码时,则第一个就会<failure xmlns=”urn:ietf:params:xml:ns:xmpp-sasl”><not-authorized/></failure>报错。提示没有注册、从登陆成功。原因是第一个设置与我opnefire服务器中的设置不一样导致。
下面看一下我openfire中的设置
版权声明:本文为sky_2016原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。