当我运行此代码时,没有迹象表明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: "[email protected]", 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: "[email protected]", hostPort: 5221, password: "password")
如果5221是正确的,你应该调用函数connect()。
self.xmppController.connect()
连接