当我运行此代码时,没有迹象表明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) {
}
}
无论出于何种原因,这都不起作用。
答案 0 :(得分:0)
您的ejabberd服务器是否正在运行。您可以通过运行./ejabberdctl status来检查ejabberd服务器的状态。另外,您可以检查ejabberd日志。检查ejabberd是否正确配置并可以正常工作的一种好方法是使用Adium登录到ejabberd服务器。
在此部分代码中检查端口是否为5222而不是5221
200 OK
如果5221是正确的,则应调用函数connect()。
try! self.xmppController = XMPPController(hostName: "192.168.0.33", userJIDString: "admin@192.168.0.33", hostPort: 5221, password: "password")
连接