• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 将 iOS 应用程序连接到 Amazon 上托管的 Ejabberd 服务器

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:38:09 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在亚马逊上托管了 Ejabberd 聊天服务器并在那里添加了两个用户,当我尝试通过 Adium 连接 Ejabberd 服务器时,它要求提供证书然后才连接,现在我正在使用 Ejabberd 服务器和 XMPP 在 Swift 中开发聊天应用程序,我配置了所有代码,传递了主机名和端口号:5222,但它没有连接到服务器。 我是否需要编写程序来获取服务器证书并将我的计算机证书 (.p12) 文件传递​​给服务器?

注意:我在 localhost 中并通过 iOS Swift 代码配置了 Ejabberd 服务器,当我从 iOS 应用程序发送消息然后它显示在 Adium 中并且当 Adium 用户发送消息然后我转到 Ejabberd 时,它运行良好Web 管理面板,可以查看离线消息。

这是连接亚马逊托管的 Ejabberd 的 Swift 代码:

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



Best Answer-推荐答案


最后我设法解决了这个问题,我真的很惊讶使用托管在亚马逊上的 Ejabberd 服务器来实现聊天应用程序。

问题:

1.当我运行我的应用程序时,它没有连接?

答案:证书问题(服务器 - 客户端证书握手解决问题)

2.无法获取在线好友? 答:我使用 Adium 让所有用户在线,然后运行我的应用程序,它就像一个魅力。

这个应用程序对我来说很棒。 如果任何人对聊天有任何问题,请随时发表评论,我期待提供帮助。谢谢

关于ios - 将 iOS 应用程序连接到 Amazon 上托管的 Ejabberd 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41743377/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap