2016-09-08 144 views
0

我試圖做一個MUC使用xmpp_messenger_ios & XMPPFrameworkxmpp_messenger_ios雨燕SWIFT MUC

的iOS這裏是加入聊天室的代碼。

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) 
     } 
    } 
} 

}

致命錯誤:意外發現零,同時展開一個可選值

我注意到儘快當連接到聊天室時,它會獲取以前的消息,從而執行上面的代碼。

請幫我出去做一個MUC聊天室的ios。我已經搜索並沒有找到任何解決方案。

謝謝

+0

致命錯誤在哪一行? –

+0

if!OneChats.knownUserForJid(jidStr:user.jidStr){這是給出錯誤的那一行。 –

+0

「OneChats.knownUserForJid」的返回值是什麼類型? –

回答

0

我解決了這個臨時解決方案。

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之前,這可以完成。

+0

如果有人找到一個更好的解決方案,然後請張貼在這裏。 –

+0

但通過這樣做會影響單個聊天。如果您在單個聊天中發送消息,則只有刷新單個聊天后才能看到消息 –

+0

在xmpp-messenger-ios中,oneroom類已存在。如何將其用於我的目的。我無法使用該課程。如果你有任何想法讓我知道.. –

0

解纏,導致零發生在:

  • user(返回值的userForJID方法XMPPUserCoreDataStorageObject!

  • jidStr(類型是String!

調查哪一個恰好是零。的user

可能的原因是零 - 的jidmanagedObjectContext零值userForJID使用(:xmppStream:managedObjectContext)`

要找出哪一個是零,只要做到這一點:

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") } 
+0

用戶變量是零 –

+0

然後,只要找出爲什麼用戶變量爲零。我已經解釋了這些可能性。對'jid'和'managedObjectContext'做同樣的事情 –

0

我想你需要首先了解XMPP MUC,請閱讀​​。

當您向MUCRoom發送消息時,服務器會將消息廣播給所有成員,包括您自己。

而這裏message.from() = room.jid但不是user.jid

這就是爲什麼user你試圖從名冊中獲得nil