陈斌彬的技术博客

Stay foolish,stay hungry

XMPPFrameworks 添加 AutoPing

为了监听服务器是否有效,增加心跳监听。用XEP-0199协议,在XMPPFrameWork框架下,封装了 XMPPAutoPing 和 XMPPPing两个类都可以使用,因为XMPPAutoPing已经组合进了XMPPPing类,所以XMPPAutoPing使用起来更方便。

首先,导入头文件

#import "XMPPAutoPing.h"

声明成员变量和属性

@interface XMPPConnectManeger : NSObject<XMPPAutoPingDelegate>

{

     XMPPAutoPing *_xmppAutoPing;

}

@property  (nonatomic, retain) XMPPAutoPing *xmppAutoPing;

在实现文件中

@synthesize xmppStream = _xmppStream;

@synthesize xmppAutoPing = _xmppAutoPing;

//初始化并启动ping

-(void)autoPingProxyServer:(NSString*)strProxyServer

{

    _xmppAutoPing = [[XMPPAutoPingalloc] init];

    [_xmppAutoPingactivate:_xmppStream];

    [_xmppAutoPingaddDelegate:selfdelegateQueue:  dispatch_get_main_queue()];

    _xmppAutoPing.respondsToQueries = YES;

    _xmppAutoPing.pingInterval=2;//ping 间隔时间

    if (nil != strProxyServer)

    {

       _xmppAutoPing.targetJID = [XMPPJID jidWithString: strProxyServer ];//设置ping目标服务器,如果为nil,则监听socketstream当前连接上的那个服务器

    }

}

//卸载监听

 [_xmppAutoPing   deactivate];

  [_xmppAutoPing   removeDelegate:self];

   _xmppAutoPing = nil;

//ping XMPPAutoPingDelegate的委托方法:

- (void)xmppAutoPingDidSendPing:(XMPPAutoPing *)sender

{

    NSLog(@"- (void)xmppAutoPingDidSendPing:(XMPPAutoPing *)sender");

}

- (void)xmppAutoPingDidReceivePong:(XMPPAutoPing *)sender

{

    NSLog(@"- (void)xmppAutoPingDidReceivePong:(XMPPAutoPing *)sender");

}



- (void)xmppAutoPingDidTimeout:(XMPPAutoPing *)sender

{

    NSLog(@"- (void)xmppAutoPingDidTimeout:(XMPPAutoPing *)sender");

}