*  XMPP的特点,所有的请求都是通过代理的方式实现的
 *
 *  因为xmpp是经由网络服务器进行数据通讯的,因此所有的,因此所有的请求都是提交给服务器处理
 *
 *  服务器处理完毕止呕,以代理的方式告诉客户端处理结果
 *
 *  官方推荐AppDelegate处理所有来自XMPP服务器的代理响应

AppDelegate.h

//
// AppDelegate.h
// xmpp练习
//
// Created by tqh on 15/4/12.
// Copyright (c) 2015年 tqh. All rights reserved.
// #import <UIKit/UIKit.h>
#import "XMPPFramework.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; #pragma mark - XMPP相关的属性和方法定义
/**
* 全局xmppstream,只读属性
*/
@property (strong,nonatomic,readonly) XMPPStream *xmppStream; @end

AppDelegate.m

//
// AppDelegate.m
// xmpp练习
//
// Created by tqh on 15/4/12.
// Copyright (c) 2015年 tqh. All rights reserved.
// #import "AppDelegate.h" //提示,此处不遵守XMPPStreamDlegate协议,程序仍然能够正常运行
@interface AppDelegate ()<XMPPStreamDelegate>
/**
* 设置xmppStream
*/
- (void)setupStream; /**
* 通知服务器用户上线
*/
- (void)goOneline; /**
* 通知服务器用户下线
*/
- (void)goOffline; /**
* 链接服务器
*/
- (void)connect; /**
* 与服务器断开连接
*/ - (void)disConnect; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// [self connect];
return YES;
} - (void)applicationWillResignActive:(UIApplication *)application {
[self disConnect];
} - (void)applicationDidBecomeActive:(UIApplication *)application { [self connect];
} #pragma mark - XMPP相关方法 #pragma mark - 设置XMPPStream - (void)setupStream {
//避免被重复实例化
if (_xmppStream == nil) {
//1.实例化XMPPStream
_xmppStream = [[XMPPStream alloc]init];
//2.添加代理
//因为所有网络请求都是做基于网络数据处理,跟UI界面无关,因此可以让代理方法在其它线程中运行
//从而提高程序的运行性能
[_xmppStream addDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, )];
} } #pragma mark - 通知服务器用户上线 - (void)goOneline {
//1.实例化一个"展现",上线的报告
XMPPPresence *presence = [XMPPPresence presence];
//2.发送presence给服务器
//服务器知道"我"上线后,只需要通知我的好友,而无需通知我,因此,次方法没有回调
[_xmppStream sendElement:presence]; } #pragma mark - 通知服务器用户下线 - (void)goOffline {
NSLog(@"用户下线");
//1.实例化一个“展现”,下线报告
XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
//2.发送Presence给服务器,通知服务器客户端下线
[_xmppStream sendElement:presence];
} #pragma mark - 连接 - (void)connect {
//1.设置XMPPStream
[self setupStream];
//2.设置用户名,密码,主机(服务器),连接时不需要password
NSString *userName = @"tqhTest"; NSString *hostName = @"127.0.0.1";
//3.设置XMPPStream的JID和主机
// [_xmppStream setMyJID:[XMPPJID jidWithString:userName]]; //@127.0.0.1
[_xmppStream setMyJID:[XMPPJID jidWithUser:userName domain:@"127.0.0.1" resource:nil]];
[_xmppStream setHostName:hostName];
//4.开始链接
NSError *error = nil;
[_xmppStream connectWithTimeout: error:&error];
//提示:如果没有制定JID和hostName,才会出错,其他都不会出错
if (error) {
NSLog(@"连接请求发送出错:%@",error.localizedDescription);
}else {
NSLog(@"连接请求发送成功");
}
} #pragma mark - 断开连接 - (void)disConnect {
//1.通知服务器下线
[self goOffline];
//2.XMPPStream断开连接
[_xmppStream disconnect];
} #pragma mark - 代理方法
#pragma mark - 连接完成(如果服务器地址不对,就不会调用此方法)
- (void)xmppStreamDidConnect:(XMPPStream *)sender {
NSLog(@"连接建立");
//开始发送身份验证请求
NSError *error = nil;
NSString *password = @"";
[_xmppStream authenticateWithPassword:password error:&error]; } #pragma mark - 身份验证通过 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
NSLog(@"身份验证通过");
} #pragma mark - 密码错误,身份验证失败 - (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error { NSLog(@"身份验证失败%@",error); }
//<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized></not-authorized></failure>
@end

最新文章

  1. Maximum Depth of Binary Tree
  2. nyoj------79拦截导弹
  3. magento问题集
  4. eclipse 中maven项目右键没有maven菜单问题
  5. YII Framework学习教程-YII的日志
  6. matlab查找回车字符
  7. Visual EmbedLinux Tools:让vs支持嵌入式Linux开发(转)
  8. Autolayout-VFL语言添加约束-备
  9. java时间格式转换
  10. Eclipse tomcat插件
  11. 我的Python之路【第一篇】:Python简介和入门
  12. 如何将JAR包发布到Maven中央仓库?
  13. chrome浏览器onunload方法无法执行window.location.href
  14. 记使用talend从oracle抽取数据时,数字变为0的问题
  15. (转)Go中的string和[]byte对比
  16. Python之路,第七篇:Python入门与基础7
  17. 用SVN进行团队开发协作生命周期详解
  18. fedora arm-linux-gcc4.4.3编译u-boot-2010.3
  19. Hystrix之Dashboard的常见问题
  20. (四)SpringMvc文件上传

热门文章

  1. FusionCharts 3.2.1 常用用法
  2. codevs 1742 爬楼梯(水题日常)
  3. Kubernetes里的ConfigMap的用途
  4. CF 1119F Niyaz and Small Degrees
  5. 五、Pandas玩转数据
  6. 用navcat编写定时任务调用存储过程
  7. nonrepetitive DNA|repetitive DNA|moderaly repetitive DNA|highly repetitive DNA|selfish gene|junk DNA
  8. css3中animation属性animation-timing-function知识点以及其属性值steps()
  9. jquery操作滚动条滚动到指定元素位置 scrollTop
  10. 实现类似AOP的封装和配置