XMPPFramework (Swift) 的问题
Posted
技术标签:
【中文标题】XMPPFramework (Swift) 的问题【英文标题】:Issue with XMPPFramework (Swift) 【发布时间】:2018-09-13 14:03:51 【问题描述】:当我运行此代码时,没有迹象表明 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)
无论出于何种原因,这都不起作用。
【问题讨论】:
【参考方案1】:您的 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()
连接
【讨论】:
以上是关于XMPPFramework (Swift) 的问题的主要内容,如果未能解决你的问题,请参考以下文章