首页 > 学院 > 开发设计 > 正文

xmpp与服务器连接并身份验证成功

2019-11-14 19:16:27
字体:
来源:转载
供稿:网友

 *  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, 0)];    }    }#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:10 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 = @"123456";    [_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

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表