XMPP使用淺談;iOS開發。

半年前幫朋友公司寫了一款App,實現通訊功能時非要用XMPP。於是開始研究,當時沒有及時總結,近期公司稍微空閒,於是抽出時間來聊聊這個框架。

首先我們去gitHub下載相關的資源。

//xmpp底層是socket,是一種長鏈接形式,如果不主動斷開,是不會斷開的。


接下來就開始使用了:主要用到的一些東西:XMPPStream、XMPPRoster、XMPPMessageArchiving、XMPPJID、XMPPPresence。


使用之初我先創建了一個工具類來處理xmpp相關的操作。
1.創建通道XMPPStream並打開配置服務地址(hostName)和服務器端口(hostPort)設置代理。
2.創建花名冊XMPPRoster 我們通過花名冊來獲取好友列表,新增好友等等;記得激活通道哦。
3.創建信息歸檔處理的對象XMPPMessageArchiving用它來處理信息。


其中我們也用到了xmpp的代理方法
#pragma mark XMPPStreamDelegate
- (void)xmppStreamWillConnect:(XMPPStream *)sender {
   NSLog(@"通道將要鏈接");
}

- (void)xmppStreamDidConnect:(XMPPStream *)sender {
   NSLog(@"通道已經鏈接");
}
#pragma mark - 註冊成功的回調方法
- (void)xmppStreamDidRegister:(XMPPStream *)sender {
   
   NSLog(@"註冊成功");
   
}

#pragma mark - 註冊失敗的回調方法
- (void)xmppStream:(XMPPStream *)senderdidNotRegister:(DDXMLElement *)error {
   
   NSLog(@"註冊失敗");
   
}





//登錄失敗
- (void)xmppStream:(XMPPStream *)senderdidNotAuthenticate:(DDXMLElement *)error {
   
   NSLog(@"登錄失敗");
   
}

- (void)xmppStream:(XMPPStream *)sendersocketDidConnect:(GCDAsyncSocket *)socket {
   
   //xmpp底層是socket,是一種長鏈接形式,如果不主動斷開,是不會斷開的
   
   
   NSLog(@"socket");
}

登錄註冊時創建XMPPJID來模型化用戶  參數1.用戶名2.域名3.標識
XMPPJID *loginJid = [XMPPJID jidWithUser:userName domain:kDomin resource:kResource ];
    self.xmppStream.myJID = loginJid;



//取消數據發送
- (void)connectToServer {
    if([self.xmppStream isConnected] || [self.xmppStream isConnecting]){
       //取消上線狀態
       XMPPPresence *presence = [XMPPPresencepresenceWithType:@"unavailable"];
       //告訴通道下線
       [self.xmppStream sendElement:presence];
       //取消鏈接
       [self.xmppStream disconnect];
       
    }
    NSError*error = nil;
   [self.xmppStream connectWithTimeout:-1 error:&error];
}

添加好友:
- (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence {
    ///去的好友狀態
//    NSString *presenceType = [NSString stringWithFormat:@"%@",[presence type]];///online, offline
    ///請求的用戶
    NSString *presenceFromUser = [NSString stringWithFormat:@"%@",[[presence from] user]];
    XMPPJID *jid = [XMPPJID jidWithString:presenceFromUser];
    //接收添加好友請求
    [_xmppRoster acceptPresenceSubscriptionRequestFrom:jid andAddToRoster:YES];
    
}
-(void)goOnline{
    
    //發送在線狀態
    XMPPPresence *presence = [XMPPPresence presence];
    [[self xmppStream] sendElement:presence];
    
}
-(void)goOffline{
    
    //發送下線狀態
    XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
    [[self xmppStream] sendElement:presence];
    
}
//鏈接服務器
-(BOOL)connect{

    //從本地取得用戶名,密碼和服務器地址
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSString *userId = [defaults stringForKey:self.model.telephone];

    NSString *pass = [defaults stringForKey:self.model.picUrl];
    NSString *server = [defaults stringForKey:kHostName1];
    
    if (![_xmppStream isDisconnected]) {
        return YES;
    }
    
    if (userId == nil || pass == nil) {
        return NO;
    }
    
    //設置用戶
    [_xmppStream setMyJID:[XMPPJID jidWithString:userId]];
    //設置服務器
    [_xmppStream setHostName:server];

    
    //連接服務器
    [self connectToServer];
    
    return YES;
    
}
-(void)disconnect{
    
    [self goOffline];
    [_xmppStream disconnect];
    
}

//下面是聊天時發送消息
NSString *siID = [XMPPStream generateUUID];
    
    XMPPMessage *xmppMessage = [XMPPMessage messageWithType:@"chat" to:self.jid elementID:siID];
    NSXMLElement *receipt = [NSXMLElement elementWithName:@"requst" xmlns:@"urn:xmpp:receipts"];
    [xmppMessage addChild:receipt];
    [xmppMessage addBody:textField.text];
    [[ManagerData shareManagerData].xmppStream sendElement:xmppMessage];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章