為了監聽伺服器是否有效,增加心跳監聽。用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");
}