2017-01-19 9 views
2

AmazonでEjabberdチャットサーバーをホストし、2人のユーザーを追加しました。AdiumでEjabberdサーバーに接続しようとしたときに証明書が要求され、チャットアプリケーションSwiftでEjabberdサーバーとXMPPを使用して、ホスト名とポート番号:5222を渡したが、サーバーに接続していないすべてのコードを構成しました。 サーバー証明書を取得し、サーバーにコンピュータ証明書(.p12)ファイルを渡すプログラムを作成する必要がありますか?AmazonでホストされているEjabberdサーバーにiOSアプリケーションを接続

:私はローカルホストにEjabberdサーバを設定し、私はiOSアプリからメッセージを送信するときのiOSスウィフトコードを通じて、それは完璧に働いて、それはAdiumのに示し、Adiumのユーザーがメッセージを送信するとき、私はEjabberd Web管理パネルに移動し、オフラインメッセージをチェックできます。ここで

がEjabberdがアマゾンでホストされている接続用スウィフトのコードです:

// 
// AppDelegate.swift 
// Thanks to Process One for this. 

import UIKit 
import XMPPFramework 

protocol ChatDelegate { 
    func buddyWentOnline(_ name: String) 
    func buddyWentOffline(_ name: String) 
    func didDisconnect() 
} 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate, XMPPRosterDelegate, XMPPStreamDelegate { 

    var window: UIWindow? 
    var delegate:ChatDelegate! = nil 
    let xmppStream = XMPPStream() 
    let xmppRosterStorage = XMPPRosterCoreDataStorage() 
    var xmppRoster: XMPPRoster 

    override init() { 
     xmppRoster = XMPPRoster(rosterStorage: xmppRosterStorage) 
    } 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 

     DDLog.add(DDTTYLogger.sharedInstance()) 

     setupStream() 

     return true 
    } 

    func applicationWillResignActive(_ application: UIApplication) { 
     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
     disconnect() 
    } 

    func applicationDidEnterBackground(_ application: UIApplication) { 
     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
    } 

    func applicationWillEnterForeground(_ application: UIApplication) { 
     // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 
    } 

    func applicationDidBecomeActive(_ application: UIApplication) { 
     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
     connect() 
    } 

    func applicationWillTerminate(_ application: UIApplication) { 
     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
    } 

    //MARK: Private Methods 
    func setupStream() {//fileprivate 
     //xmppRoster = XMPPRoster(rosterStorage: xmppRosterStorage) 
     xmppRoster.activate(xmppStream) 
     xmppStream?.addDelegate(self, delegateQueue: DispatchQueue.main) 
     xmppRoster.addDelegate(self, delegateQueue: DispatchQueue.main) 
    } 

    func goOnline() { //fileprivate 
     let presence = XMPPPresence() 
     let domain = xmppStream?.myJID.domain 

     if domain == "gmail.com" || domain == "gtalk.com" || domain == "talk.google.com" { 
      let priority = DDXMLElement.element(withName: "priority", stringValue: "24") as! DDXMLElement 
      presence?.addChild(priority) 
     } 

     xmppStream?.send(presence) 
    } 

    func goOffline() { //fileprivate 
     let presence = XMPPPresence(type: "unavailable") 
     xmppStream?.send(presence) 
    } 

    func connect() -> Bool { 
     xmppStream.hostName = "amazon hosted ejabber ip address" 
     xmppStream.hostPort=5222 
     if !(xmppStream?.isConnected())! { 
      let jabberID = UserDefaults.standard.string(forKey: "userID") 
      let myPassword = UserDefaults.standard.string(forKey: "userPassword") 

      if !(xmppStream?.isDisconnected())! { 
       return true 
      } 
      if jabberID == nil && myPassword == nil { 
       return false 
      } 


      xmppStream?.myJID = XMPPJID.init(string: jabberID) 

      do { 
       try xmppStream?.connect(withTimeout: XMPPStreamTimeoutNone) 
       print("Connection success") 
       return true 
      } catch { 
       print("Something went wrong!") 
       return false 
      } 
     } else { 
      return true 
     } 
    } 

    func disconnect() { 
     goOffline() 
     xmppStream?.disconnect() 
    } 

    //MARK: XMPP Delegates 
    func xmppStreamDidConnect(_ sender: XMPPStream!) { 
     do { 
      try xmppStream?.authenticate(withPassword: UserDefaults.standard.string(forKey: "userPassword")) 
     } catch { 
      print("Could not authenticate") 
     } 
    } 

    func xmppStreamDidAuthenticate(_ sender: XMPPStream!) { 
     goOnline() 
    } 

    func xmppStream(_ sender: XMPPStream!, didReceive iq: XMPPIQ!) -> Bool { 
     print("Did receive IQ") 
     return false 
    } 

    func xmppStream(_ sender: XMPPStream!, didReceive message: XMPPMessage!) { 
     print("Did receive message \(message)") 
    } 

    func xmppStream(_ sender: XMPPStream!, didSend message: XMPPMessage!) { 
     print("Did send message \(message)") 
    } 

    func xmppStream(_ sender: XMPPStream!, didReceive presence: XMPPPresence!) { 
     let presenceType = presence.type() 
     let myUsername = sender.myJID.user 
     let presenceFromUser = presence.from().user 

     if presenceFromUser != myUsername { 
      print("Did receive presence from \(presenceFromUser)") 
      if presenceType == "available" { 
       delegate.buddyWentOnline("\(presenceFromUser)@gmail.com") 
      } else if presenceType == "unavailable" { 
       delegate.buddyWentOffline("\(presenceFromUser)@gmail.com") 
      } 
     } 
    } 

    func xmppRoster(_ sender: XMPPRoster!, didReceiveRosterItem item: DDXMLElement!) { 
     print("Did receive Roster item") 
    } 
} 

答えて

0

最後に、私は、この問題を解決するために管理し、私は本当にアマゾンでホストされているEjabberdサーバーを使用してチャットアプリケーションを実装するために驚かいます。

問題:私は自分のアプリケーションを実行していたとき

1.、それは接続されませんでしたか?

回答:証明書の問題(サーバ - クライアント側の証明書のハンドシェイクが問題を解決する)オンラインの仲間を取得することができません

2.? 回答:私はすべてのユーザーをオンラインにしてからアプリケーションを実行するためにAdiumを使用しました。これは魅力的です。

このアプリケーションで私にとって素晴らしい学習です。 チャットに関する問題が発生した場合は、お気軽にご意見をお寄せください。ありがとう

+0

こんにちは@Shobhakar私はチャットアプリケーションを書いており、私はxmppに精通していないので、いくつかのことが不思議でした。サーバーを作成する必要がありますか、またはジャバーを使用できますか?迅速に使用するための良いフレームワーク/ライブラリをお勧めできますか?あなたが助けてくれたチュートリアルはあなたを助けましたか?どんな情報にも感謝してくれたり、私に与えることができます。 Sep – Septronic

+0

ejaberdサーバーを使用して、リモートアクセスでAmazonやその他の場所でejaberdをホストする必要があります。これを確認するにはSwiftデモをチェックしてください。skype –

+1

素晴らしい感謝です。あなたにskype reqを送りました。 – Septronic

関連する問題