美文网首页iOS Developer
iOS-Swift XMPP 获取好友、群聊会议室、及消息、自动

iOS-Swift XMPP 获取好友、群聊会议室、及消息、自动

作者: 走在路上的小二 | 来源:发表于2016-12-28 19:29 被阅读575次

    一、XMPPStreamDelegate 协议

    ** 上线下线**

       // 上线
       private func goOnline() -> Void {
            // 生成状态元素
            let presence: XMPPPresence = XMPPPresence(type: "available")
            self.xmppStream!.send(presence)
        }
        // 下线
        private func goOffline() -> Void {
            let presence: XMPPPresence = XMPPPresence(type: "unavailable")
            self.xmppStream!.send(presence)
        }
    
     // 断开连接成功
       func xmppStreamDidDisconnect(_ sender: XMPPStream, withError error: Error?) -> Void {     
        }
    // 连接超时
        func xmppStreamConnectDidTimeout(_ sender: XMPPStream) -> Void {
        }
    // 连接服务器
        func xmppStreamDidConnect(_ sender: XMPPStream) -> Void {
    }
     // 验证通过
        func xmppStreamDidAuthenticate(_ sender: XMPPStream) -> Void {
    }
      // 验证不通过
        func xmppStream(_ sender: XMPPStream, didNotAuthenticate error: NSXMLElement) -> Void {
    }
     // 收到好友状态
        func xmppStream(_ sender: XMPPStream!, didReceive presence: XMPPPresence!) {
            // 取得好友状态
            let presenceType = presence.type()
            // 当前用户
            let userId = sender.myJID.user
            // 在线用户
            let presenceFromUser = presence.from().user
            if (!(presenceFromUser == userId)) {
                // 在线状态
                if presenceType == "available" {
               }  else { // 离线状态
               }
           }
      }
    // 收到消息
      func xmppStream(_ sender: XMPPStream, didReceive message:XMPPMessage) -> Void {
    }
    // 发送消息成功
     func xmppStream(_ sender: XMPPStream!, didSend message: XMPPMessage!) {
    }
    // 发送消息失败
     func xmppStream(_ sender: XMPPStream!, didFailToSend message: XMPPMessage!, error: 
    }
    

    二、XMPPRosterDelegate 协议

    ** 获取好友**

       func xmppRosterDidBeginPopulating(_ sender: XMPPRoster, withVersion version: String) -> Void {
            // print("开始获取好友 %s,%d", #function, #line)
        }
        func xmppRosterDidEndPopulating(_ sender: XMPPRoster) -> Void {
            // print("获取好友结束 %s,%d", #function, #line)
        }
        // 一次获取一个好友
        func xmppRoster(_ sender: XMPPRoster, didReceiveRosterItem item: DDXMLElement) -> Void {
            // print("获取好友 %s %d", #function, #line)
            // print("好友信息 %@",item);
        }
        func xmppRoster(_ sender: XMPPRoster, didReceiveRosterPush iq: XMPPIQ) -> Void {
    }
        func xmppRoster(_ sender: XMPPRoster, didReceivePresenceSubscriptionRequest presence:XMPPPresence) -> Void {
            print("获取好友请求")
    }
    // 删除好友
        func removeFriendWithFriendName(friendName: String) -> Void {
            let friendJid = XMPPJID(string: friendName)
            self.xmppRoster!.removeUser(friendJid)
        }
        // 添加好友
        func addFriendWithUserId(userId: String?, friendName: String, groupName: String?) {
            let nickName: String? = friendName.isEmpty ? nil : friendName
            let groupArray: Array? = groupName == "我的好友" ? nil : [groupName!]
            let friendJid: XMPPJID = XMPPJID(user: userId, domain: kDomain, resource: "iOS")
            self.xmppRoster!.addUser(friendJid, withNickname: nickName, groups: groupArray, subscribeToPresence: true)
        }
    

    三、XMPPReconnectDelegate协议

    切换网络自动连接网络

    // MARK: XMPPReconnectDelegate
        func xmppReconnect(_ sender: XMPPReconnect!, shouldAttemptAutoReconnect connectionFlags: SCNetworkReachabilityFlags) -> Bool {
            return true
        }
        func xmppReconnect(_ sender: XMPPReconnect!, didDetectAccidentalDisconnect connectionFlags: SCNetworkReachabilityFlags) {
            print("%u", connectionFlags) // 断开连接
            self.noNetWork = true
        }
    // 然后再XMPPStreamDelegate协议断开连接的方法里面手动连接网络
        // 断开连接成功
        func xmppStreamDidDisconnect(_ sender: XMPPStream, withError error: Error?) -> Void {
            if self.noNetWork {
                self.xmppReconnect!.manualStart()
                self.noNetWork = false
            }
        }
    

    四、XMPPRoomDelegate协议

    聊天室

    // MARK: XMPPRoomDelegate
        // 群聊: 收到消息
        func xmppRoom(_ sender: XMPPRoom!, didReceive message: XMPPMessage!, fromOccupant occupantJID: XMPPJID!) {
    }
       // 已经创建群聊
       func xmppRoomDidCreate(_ sender: XMPPRoom!) {
    }
      // 已加入聊天室
       func xmppRoomDidJoin(_ sender: XMPPRoom!) {
    }
      // 已经离开聊天室
      func xmppRoomDidLeave(_ sender: XMPPRoom!) {
    }
     // 设定群聊
     func xmppRoom(_ sender: XMPPRoom!, didConfigure iqResult:   XMPPIQ!) {  
    }
     // 群聊: 成员变动
      func xmppRoom(_ sender: XMPPRoom!, occupantDidUpdate occupantJID: XMPPJID!, with presence: XMPPPresence!) {
    }
      // 收到加入聊天室邀请
      func xmppMUC(_ sender: XMPPMUC!, roomJID: XMPPJID!, didReceiveInvitation message: XMPPMessage!) {
            print("inviter :\(roomJID) send invitation")
     }
     // 取得房间列表
      func xmppMUC(_ sender: XMPPMUC!, didDiscoverRooms rooms: [Any]!, forServiceNamed serviceName: String!) {
    }
    
    PS: 有什么疑问欢迎留言。
    

    相关文章

      网友评论

        本文标题:iOS-Swift XMPP 获取好友、群聊会议室、及消息、自动

        本文链接:https://www.haomeiwen.com/subject/rqzyvttx.html