当我运行此代码时,没有迹象表明 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/