Reputation: 89
There is no indication that the XMPPController has connected to the Ejabberd server when I run this code. The XMPPStreamDelegate methods are not being called either. It is as though the code isn't even there. There should be some indication I'm connected shouldn't there? Anyone else had this issue? This is my code. Thanks.
XMPPController class:
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 (for testing 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) {
}
}
For whatever reason this does not work.
Upvotes: 1
Views: 161
Reputation: 1359
Is your ejabberd server working. You can check status of ejabberd server by running ./ejabberdctl status. Also, can you check ejabberd logs. A good way to check if your ejabberd is configured correctly and working is to log in to your ejabberd server using Adium.
Check if the port is 5222 instead of 5221 in this part of the code
try! self.xmppController = XMPPController(hostName: "192.168.0.33", userJIDString: "[email protected]", hostPort: 5221, password: "password")
if 5221 is correct you should call the function connect().
self.xmppController.connect()
to connect
Upvotes: 1