swift - XMPPFramework (Swift) 问题

标签 swift xmpp xmppframework

当我运行此代码时,没有迹象表明 XMPPController 已连接到 Ejabberd 服务器。 XMPPStreamDelegate 方法也没有被调用。就好像代码根本不存在一样。应该有一些迹象表明我已连接,不是吗?还有其他人遇到这个问题吗?这是我的代码。谢谢。

XMPPController 类:

import Foundation
import XMPPFramework

enum XMPPControllerError: Error {

case wrongUserJID

}

class XMPPController: NSObject {

var xmppStream: XMPPStream

let hostName: String
let userJID: XMPPJID
let hostPort: UInt16
let password: String

init(hostName: String, userJIDString: String, hostPort: UInt16, password: String) throws {
    guard let userJID = XMPPJID(string: userJIDString) else {
        throw XMPPControllerError.wrongUserJID
    }


    self.hostName = hostName
    self.userJID = userJID
    self.hostPort = hostPort
    self.password = password

    self.xmppStream = XMPPStream()
    self.xmppStream.hostName = hostName
    self.xmppStream.hostPort = hostPort
    //self.xmppStream.startTLSPolicy = XMPPStreamStartTLSPolicy.allowed
    self.xmppStream.enableBackgroundingOnSocket = true
    self.xmppStream.myJID = userJID


    super.init()
    self.xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)



}



func connect() {


    if !self.xmppStream.isDisconnected() {
        return
    }

    do {

        try self.xmppStream.connect(withTimeout: 5)

    } catch {

        print("ERROR CONNECTING")

    }

}

}

extension XMPPController: XMPPStreamDelegate {


func xmppStreamDidConnect(_ sender: XMPPStream!) {
    print("Stream: Connected")
    try! sender.authenticate(withPassword: self.password)
}

func xmppStreamDidAuthenticate(_ sender: XMPPStream!) {
    self.xmppStream.send(XMPPPresence())
    print("Stream: Authenticated")
}

}

AppDelegate(用于测试 XMPPController)

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var xmppController: XMPPController!

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

    try! self.xmppController = XMPPController(hostName: "192.168.0.33", userJIDString: "admin@192.168.0.33", hostPort: 5221, password: "password")



    return true
}

func applicationWillResignActive(_ application: UIApplication) {

}

func applicationDidEnterBackground(_ application: UIApplication) {

}

func applicationWillEnterForeground(_ application: UIApplication) {

}

func applicationDidBecomeActive(_ application: UIApplication) {

}

func applicationWillTerminate(_ application: UIApplication) {

}


}

无论出于何种原因,这都不起作用。

最佳答案

您的 ejabberd 服务器正常吗?您可以通过运行 ./ejabberdctl status 检查 ejabberd 服务器的状态。另外,你能检查一下 ejabberd 日志吗?检查您的 ejabberd 是否配置正确并正常工作的一个好方法是使用 Adium 登录您的 ejabberd 服务器。

检查这部分代码中端口是否是5222而不是5221

try! self.xmppController = XMPPController(hostName: "192.168.0.33", userJIDString: "admin@192.168.0.33", hostPort: 5221, password: "password")

如果 5221 正确,您应该调用函数 connect()。

self.xmppController.connect()

连接

关于swift - XMPPFramework (Swift) 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52315470/

相关文章:

ios - 如何从 XMPPRoster 获取好友?

iOS XMPPFramework - 房间/聊天消息历史记录

swift - 如何在 OS X 中的虚拟桌面之间移动应用程序窗口?

Swift 将 CABasicAnimation 与 label.text 同步

ios - TableView 导航到 View Controller 和其他单元格中的其他操作

ios - XmppFramework 我收到 KissXml 错误

c# - 什么控件最适合好友列表?

swift - 如何找到 SKSpriteNode 的速度作为 x 和 y 值?

Android XMPP 避免资源冲突

ios - xmppframework - 为发送/接收启用日志