xmpp_messenger_ios Swift MUC swift

xmpp_messenger_ios Swift MUC swift

我正在尝试使用 xmpp_messenger_ios 和 XMPPFramework

在 iOS 上制作 MUC

这是加入房间的代码。

   func createOrJoinRoomOnXMPP(){
    // location has named array of lat and long

    NSLog("Creating room on XMPP")

    let roomJID: XMPPJID = XMPPJID.jidWithString(self.roomID + "@conference.ip-172-31-41-100")

    let roomData: XMPPRoomCoreDataStorage = XMPPRoomCoreDataStorage.sharedInstance()

    let chatRoom = XMPPRoom.init(roomStorage: roomData, jid: roomJID, dispatchQueue: dispatch_get_main_queue())

    chatRoom.activate(OneChat.sharedInstance.xmppStream)
    chatRoom.addDelegate(self, delegateQueue: dispatch_get_main_queue())

    //        let history = DDXMLElement.elementWithName("history")
    //        // Get lst messegs of the room
    //        history.addAttributeWithName("maxstanzas", stringValue: "10")

    chatRoom.joinRoomUsingNickname(OneChat.sharedInstance.xmppStream!.myJID.user, history: nil)
}

一旦执行此块,我就会在这段代码中收到错误:

扩展 OneMessage:XMPPStreamDelegate {

public func xmppStream(sender: XMPPStream, didSendMessage message: XMPPMessage) {
    if let completion = OneMessage.sharedInstance.didSendMessageCompletionBlock {
        completion(stream: sender, message: message)
    }
    //OneMessage.sharedInstance.didSendMessageCompletionBlock!(stream: sender, message: message)
}

public func xmppStream(sender: XMPPStream, didReceiveMessage message: XMPPMessage) {
    let user = OneChat.sharedInstance.xmppRosterStorage.userForJID(message.from(), xmppStream: OneChat.sharedInstance.xmppStream, managedObjectContext: OneRoster.sharedInstance.managedObjectContext_roster())

    if !OneChats.knownUserForJid(jidStr: user.jidStr) { // <<< ERROR LINE
        OneChats.addUserToChatList(jidStr: user.jidStr)
    }

    if message.isChatMessageWithBody() {
        OneMessage.sharedInstance.delegate?.oneStream(sender, didReceiveMessage: message, from: user)
    } else {
        //was composing
        if let _ = message.elementForName("composing") {
            OneMessage.sharedInstance.delegate?.oneStream(sender, userIsComposing: user)
        }
    }
}

}

致命错误:在展开可选值时意外发现 nil

我注意到,一旦连接到聊天室,它就会获取以前的消息,因此会执行上面的代码。

请帮助我在 ios 上为房间聊天制作 MUC。我已经搜索过,但没有找到任何解决方案。

谢谢

我通过这个临时解决方案解决了这个问题。

extension OneMessage: XMPPStreamDelegate {

    public func xmppStream(sender: XMPPStream, didSendMessage message: XMPPMessage) {
        if let completion = OneMessage.sharedInstance.didSendMessageCompletionBlock {
            completion(stream: sender, message: message)
        }
        //OneMessage.sharedInstance.didSendMessageCompletionBlock!(stream: sender, message: message)
    }

    public func xmppStream(sender: XMPPStream, didReceiveMessage message: XMPPMessage) {
        NSLog("This is blocked")

//      let user = OneChat.sharedInstance.xmppRosterStorage.userForJID(message.from(), xmppStream: OneChat.sharedInstance.xmppStream, managedObjectContext: OneRoster.sharedInstance.managedObjectContext_roster())
//      
//      if !OneChats.knownUserForJid(jidStr: user.jidStr) {
//          OneChats.addUserToChatList(jidStr: user.jidStr)
//      }
//      
//      if message.isChatMessageWithBody() {
//          OneMessage.sharedInstance.delegate?.oneStream(sender, didReceiveMessage: message, from: user)
//      } else {
//          //was composing
//          if let _ = message.elementForName("composing") {
//              OneMessage.sharedInstance.delegate?.oneStream(sender, userIsComposing: user)
//          }
//      }
    }
}

正在阻止 OneMessage.swift 代码。

并在我的 ViewController 中处理收到的消息。

这不是正确的做法。但在 ProcessOne 支持 MUC 之前,这是可以做到的。

导致 nil 的解包发生在:

  • user(return 方法 userForJID 的值是 XMPPUserCoreDataStorageObject!

  • jidStr(类型为String!

调查哪一个恰好为零。

user 为零的可能原因 - jidmanagedObjectContext 的零值用于 userForJID(:xmppStream:managedObjectContext)`

要找出哪一个是 nil,只需这样做:

guard let user = OneChat.sharedInstance.xmppRosterStorage.userForJID(message.from(), xmppStream: OneChat.sharedInstance.xmppStream, managedObjectContext: OneRoster.sharedInstance.managedObjectContext_roster())
else { fatalError("user is nil") }

guard let userJIDStr = user.jidStr
else { fatalError("jidStr is nil") }

我认为你需要先了解 XMPP MUC,阅读这篇文章 doc

当您向MUCRoom发送消息时,服务器会将消息广播给所有成员,包括您自己。

这里 message.from() = room.jid 但不是 user.jid

这就是为什么您尝试从花名册中获得的 usernil