使用第三方框架 XMPPFramework
#import "ViewController.h"
#import "XMPPFramework.h"
@interface ViewController ()<XMPPStreamDelegate>{
XMPPStream *_stream;
XMPPRoster *_roster;
}
@end
/*
二、如何集成XMPP协议框架
1. xmpp框架使用ARC
2. 依赖的框架:
1> libxml.dylib header search Path: /usr/include/libxml2
2> libresolv.dylib
三、开源的XMPP服务器端程序
1. openfire
*/
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//管理xmpp链接的类
_stream = [[XMPPStream alloc]init];
[_stream setHostName:服务器];//服务器域名
[_stream setHostPort:5222];//可以不设置,默认5222
[_stream addDelegate:self delegateQueue:dispatch_get_main_queue()];
//重连
XMPPReconnect *reconnect = [[XMPPReconnect alloc]init];
[reconnect activate:_stream];
//花名册
XMPPRosterCoreDataStorage *storage = [[XMPPRosterCoreDataStorage alloc]init];
//好友管理
_roster = [[XMPPRoster alloc]initWithRosterStorage:storage];
[_roster addDelegate:self delegateQueue:dispatch_get_main_queue()];
[_roster activate:_stream];
}
//注册
- (IBAction)registerAction:(id)sender {
XMPPJID *jid = [XMPPJID jidWithString:@"anonymous"];//匿名链接先
_stream.myJID = jid;
BOOL isSuccess = [_stream connectWithTimeout:-1 error:nil];
if (isSuccess == NO) {
NSLog(@"建立链接失败");
}
}
// 登陆
- (IBAction)loginAction:(id)sender {
XMPPJID *jid = [XMPPJID jidWithString:@"sui_1"];
_stream.myJID = jid;
BOOL isSuccess = [_stream connectWithTimeout:-1 error:nil];
if (!isSuccess) {
NSLog(@"登陆失败");
}
}
//登陆成功调用的代理
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender{
NSLog(@"登陆成功");
//设置登陆状态--在线
XMPPPresence *presence = [[XMPPPresence alloc]init];
[_stream sendElement:presence];
//设置登陆状态--离线
// XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
// [_stream sendElement:presence];
}
//建立链接 成功后 才调用
- (void)xmppStreamDidConnect:(XMPPStream *)sender{
/* //注册
XMPPJID *jid = [XMPPJID jidWithString:@"sui_3"];//设置注册名
[_stream setMyJID:jid];
BOOL isSuccess = [_stream registerWithPassword:@"password" error:nil];//设置密码
if (isSuccess == NO) {
NSLog(@"链接失败");
}
*/
//----------------------------------------------------------------
//登陆
XMPPJID *jid = [XMPPJID jidWithString:@"sui_1"];//登陆ID
[_stream setMyJID:jid];
BOOL isSuccess = [_stream authenticateWithPassword:@"password" error:nil];//登陆密码
if (!isSuccess) {
NSLog(@"登陆失败");
}
}
- (void)xmppStreamDidRegister:(XMPPStream *)sender{
NSLog(@"注册成功");
}
/*
XML格式:
<iq type="get"
from="xiaoming@example.com"
to="example.com"
id="1234567">
<query xmlns="jabber:iq:roster"/>
<iq />
type 属性,说明了该 iq 的类型为 get,与 HTTP 类似,向服务器端请求信息
from 属性,消息来源,这里是你的 JID
to 属性,消息目标,这里是服务器域名
id 属性,标记该请求 ID,当服务器处理完毕请求 get 类型的 iq 后,响应的 result 类型 iq 的 ID 与 请求 iq 的 ID 相同
<query xmlns="jabber:iq:roster"/> 子标签,说明了客户端需要查询 roster
*/
//获取好友列表
- (IBAction)getFriendsListAction:(id)sender {
//创建一个IQ便签
DDXMLElement *element_iq = [DDXMLElement elementWithName:@"iq"];
[element_iq addAttributeWithName:@"type" stringValue:@"get"];
[element_iq addAttributeWithName:@"from" stringValue:_stream.myJID.description];
[element_iq addAttributeWithName:@"to" stringValue:_stream.myJID.domain];
[element_iq addAttributeWithName:@"id" stringValue:@"sui"];
// DDXMLElement *query = [DDXMLElement elementWithName:@"query" xmlns:@"jabber:iq:roster"];
//或者
DDXMLElement *element_query = [DDXMLElement elementWithName:@"query"];
[element_query addAttributeWithName:@"xmlns" stringValue:@"jabber:iq:roster"];
[element_iq addChild:element_query];
[_stream sendElement:element_iq];
}
/*
XML格式:
一个 IQ 响应:
<iq type="result"
id="1234567"
to="xiaoming@example.com">
<query xmlns="jabber:iq:roster">
<item jid="xiaoyan@example.com" name="小燕" />
<item jid="xiaoqiang@example.com" name="小强"/>
<query />
<iq />
type 属性,说明了该 iq 的类型为 result,查询的结果
<query xmlns="jabber:iq:roster"/> 标签的子标签 <item />,为查询的子项,即为 roster
item 标签的属性,包含好友的 JID,和其它可选的属性,例如昵称等。
*/
//接受成功
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq{
if ([iq.type isEqualToString:@"result"]) {
DDXMLElement *query = [iq childElement];
for (DDXMLElement *item in [query children]) {
NSString *jid = [item attributeStringValueForName:@"jid"];
NSString *name = [item attributeStringValueForName:@"name"];
NSLog(@"///%@--%@///",jid,name);
}
}
return YES;
}
- (IBAction)addFriendAction:(id)sender {
XMPPJID *jid = [XMPPJID jidWithString:@"sui_3"];//sui_1登陆,加sui_3为好友
[_roster addUser:jid withNickname:@""];
}
/**
发送的xml内容格式:
<message type="chat" to="xiaoming@example.com">
<body>Hello World!</body>
</message>
*/
//发送信息 -- 满足发送格式即可发送信息
- (IBAction)sendMessage:(id)sender {
DDXMLElement *element = [DDXMLElement elementWithName:@"message"];
[element addAttributeWithName:@"type" stringValue:@"chat"];
[element addAttributeWithName:@"to" stringValue:@"sui_3"];
DDXMLElement *element_body = [DDXMLElement elementWithName:@"body" stringValue:@"你好啊"];//待发送内容
[element addChild:element_body];
[_stream sendElement:element];
}
//已经接收到信息后调用的代理
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
NSLog(@"::::%@",message);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#import "XMPPFramework.h"
@interface ViewController ()<XMPPStreamDelegate>{
XMPPStream *_stream;
XMPPRoster *_roster;
}
@end
/*
二、如何集成XMPP协议框架
1. xmpp框架使用ARC
2. 依赖的框架:
1> libxml.dylib header search Path: /usr/include/libxml2
2> libresolv.dylib
三、开源的XMPP服务器端程序
1. openfire
*/
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//管理xmpp链接的类
_stream = [[XMPPStream alloc]init];
[_stream setHostName:服务器];//服务器域名
[_stream setHostPort:5222];//可以不设置,默认5222
[_stream addDelegate:self delegateQueue:dispatch_get_main_queue()];
//重连
XMPPReconnect *reconnect = [[XMPPReconnect alloc]init];
[reconnect activate:_stream];
//花名册
XMPPRosterCoreDataStorage *storage = [[XMPPRosterCoreDataStorage alloc]init];
//好友管理
_roster = [[XMPPRoster alloc]initWithRosterStorage:storage];
[_roster addDelegate:self delegateQueue:dispatch_get_main_queue()];
[_roster activate:_stream];
}
//注册
- (IBAction)registerAction:(id)sender {
XMPPJID *jid = [XMPPJID jidWithString:@"anonymous"];//匿名链接先
_stream.myJID = jid;
BOOL isSuccess = [_stream connectWithTimeout:-1 error:nil];
if (isSuccess == NO) {
NSLog(@"建立链接失败");
}
}
// 登陆
- (IBAction)loginAction:(id)sender {
XMPPJID *jid = [XMPPJID jidWithString:@"sui_1"];
_stream.myJID = jid;
BOOL isSuccess = [_stream connectWithTimeout:-1 error:nil];
if (!isSuccess) {
NSLog(@"登陆失败");
}
}
//登陆成功调用的代理
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender{
NSLog(@"登陆成功");
//设置登陆状态--在线
XMPPPresence *presence = [[XMPPPresence alloc]init];
[_stream sendElement:presence];
//设置登陆状态--离线
// XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
// [_stream sendElement:presence];
}
//建立链接 成功后 才调用
- (void)xmppStreamDidConnect:(XMPPStream *)sender{
/* //注册
XMPPJID *jid = [XMPPJID jidWithString:@"sui_3"];//设置注册名
[_stream setMyJID:jid];
BOOL isSuccess = [_stream registerWithPassword:@"password" error:nil];//设置密码
if (isSuccess == NO) {
NSLog(@"链接失败");
}
*/
//----------------------------------------------------------------
//登陆
XMPPJID *jid = [XMPPJID jidWithString:@"sui_1"];//登陆ID
[_stream setMyJID:jid];
BOOL isSuccess = [_stream authenticateWithPassword:@"password" error:nil];//登陆密码
if (!isSuccess) {
NSLog(@"登陆失败");
}
}
- (void)xmppStreamDidRegister:(XMPPStream *)sender{
NSLog(@"注册成功");
}
/*
XML格式:
<iq type="get"
from="xiaoming@example.com"
to="example.com"
id="1234567">
<query xmlns="jabber:iq:roster"/>
<iq />
type 属性,说明了该 iq 的类型为 get,与 HTTP 类似,向服务器端请求信息
from 属性,消息来源,这里是你的 JID
to 属性,消息目标,这里是服务器域名
id 属性,标记该请求 ID,当服务器处理完毕请求 get 类型的 iq 后,响应的 result 类型 iq 的 ID 与 请求 iq 的 ID 相同
<query xmlns="jabber:iq:roster"/> 子标签,说明了客户端需要查询 roster
*/
//获取好友列表
- (IBAction)getFriendsListAction:(id)sender {
//创建一个IQ便签
DDXMLElement *element_iq = [DDXMLElement elementWithName:@"iq"];
[element_iq addAttributeWithName:@"type" stringValue:@"get"];
[element_iq addAttributeWithName:@"from" stringValue:_stream.myJID.description];
[element_iq addAttributeWithName:@"to" stringValue:_stream.myJID.domain];
[element_iq addAttributeWithName:@"id" stringValue:@"sui"];
// DDXMLElement *query = [DDXMLElement elementWithName:@"query" xmlns:@"jabber:iq:roster"];
//或者
DDXMLElement *element_query = [DDXMLElement elementWithName:@"query"];
[element_query addAttributeWithName:@"xmlns" stringValue:@"jabber:iq:roster"];
[element_iq addChild:element_query];
[_stream sendElement:element_iq];
}
/*
XML格式:
一个 IQ 响应:
<iq type="result"
id="1234567"
to="xiaoming@example.com">
<query xmlns="jabber:iq:roster">
<item jid="xiaoyan@example.com" name="小燕" />
<item jid="xiaoqiang@example.com" name="小强"/>
<query />
<iq />
type 属性,说明了该 iq 的类型为 result,查询的结果
<query xmlns="jabber:iq:roster"/> 标签的子标签 <item />,为查询的子项,即为 roster
item 标签的属性,包含好友的 JID,和其它可选的属性,例如昵称等。
*/
//接受成功
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq{
if ([iq.type isEqualToString:@"result"]) {
DDXMLElement *query = [iq childElement];
for (DDXMLElement *item in [query children]) {
NSString *jid = [item attributeStringValueForName:@"jid"];
NSString *name = [item attributeStringValueForName:@"name"];
NSLog(@"///%@--%@///",jid,name);
}
}
return YES;
}
- (IBAction)addFriendAction:(id)sender {
XMPPJID *jid = [XMPPJID jidWithString:@"sui_3"];//sui_1登陆,加sui_3为好友
[_roster addUser:jid withNickname:@""];
}
/**
发送的xml内容格式:
<message type="chat" to="xiaoming@example.com">
<body>Hello World!</body>
</message>
*/
//发送信息 -- 满足发送格式即可发送信息
- (IBAction)sendMessage:(id)sender {
DDXMLElement *element = [DDXMLElement elementWithName:@"message"];
[element addAttributeWithName:@"type" stringValue:@"chat"];
[element addAttributeWithName:@"to" stringValue:@"sui_3"];
DDXMLElement *element_body = [DDXMLElement elementWithName:@"body" stringValue:@"你好啊"];//待发送内容
[element addChild:element_body];
[_stream sendElement:element];
}
//已经接收到信息后调用的代理
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
NSLog(@"::::%@",message);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
来源:https://www.cnblogs.com/chillytao-suiyuan/p/4835913.html