- 获取基本登录xmppStream对象
// 创建XMPPStream对象
_xmppStream = [[XMPPStream alloc] init];
// 设置代理
[_xmppStream addDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
- 连接到服务器(需要传入一个jid)
// 1.设置登录用户的jid
// resource 用户登录客户端设备登录的类型
XMPPJID *myJid = [XMPPJID jidWithUser:@"wangwu" domain:@"teacher.local" resource:@"iphone"];
_xmppStream.myJID = myJid;
// 2.设置主机地址
_xmppStream.hostName = @"127.0.0.1";
// 3.设置主机端口号 (默认就是5222,可以不用设置)
_xmppStream.hostPort = 5222;
// 4.发起连接
NSError *error = nil;
// 缺少必要的参数时就会发起连接失败 ? 没有设置jid
[_xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error];
if (error) {
NSLog(@"%@",error);
}else{
NSLog(@"发起连接成功");
}
#pragma mark -XMPPStream的代理
#pragma mark 连接建立成功
-(void)xmppStreamDidConnect:(XMPPStream *)sender{
NSLog(@"%s",__func__);
[self sendPwdToHost];
}
#pragma mark 登录成功
-(void)xmppStreamDidAuthenticate:(XMPPStream *)sender{
NSLog(@"%s",__func__);
[self sendOnline];
}
#pragma mark 登录失败
-(void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error{
NSLog(@"%s %@",__func__,error);
}```
- 连接成功,接着发送密码
(void)sendPwdToHost{
NSError *error = nil;
[_xmppStream authenticateWithPassword:@"123456" error:&error];
if (error) {
NSLog(@"%@",error);
}
}
- 登录成功发送一个 "在线消息" 给服务器
(void)sendOnline{
//XMPP框架,已经把所有的指令封闭成对象
XMPPPresence *presence = [XMPPPresence presence];
NSLog(@"%@",presence);
[_xmppStream sendElement:presence];
}```
注销流程代码
[self sendOffline];
// 2.断开与服务器的连接
[self disconncetFromHost];
// 注销方法
(void)sendOffline{
XMPPPresence *offline = [XMPPPresence presenceWithType:@"unavailable"];
[_xmppStream sendElement:offline];
}
// 断开xmpp连接
(void)disconncetFromHost{
[_xmppStream disconnect];
}