I am trying to use the XMPP framework(https://github.com/robbiehanson/XMPPFramework) using swift. I am new to swift
class ViewController: UIViewController {
Remember the connection takes some seconds to be established. Use the other delegate methods to track the state of the connection.
Set your host name and port.
Call methods as described below.
After this, delegate methods will be called and authentication will be done.
After authentication, one must send presence.
self.xmppStream.send(XMPPPresence())
private var hostName: String = "your host name"
private var hostPort: UInt16 = 5222
private var xmppStream: XMPPStream!
private var xmppReconnect: XMPPReconnect!
private var xmppRoster: XMPPRoster!
private var xmppvCardStorage: XMPPvCardCoreDataStorage!
private var xmppvCardTempModule: XMPPvCardTempModule!
private var xmppvCardAvatarModule: XMPPvCardAvatarModule!
private var xmppCapabilities: XMPPCapabilities!
private var xmppCapabilitiesStorage: XMPPCapabilitiesCoreDataStorage!
private var xmppMessageArchivingStorage: XMPPMessageArchivingCoreDataStorage!
private var xmppMessageArchivingModule: XMPPMessageArchiving!
private var xmppAutoPing: XMPPAutoPing!
private var userId = ""
private var password = ""
fileprivate func configureXMPP() {
// Stream Configuration
xmppStream = XMPPStream()
xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)
xmppStream.hostPort = hostPort
xmppStream.hostName = hostName
xmppStream.enableBackgroundingOnSocket = true
xmppStream.keepAliveInterval = 0.5;
xmppStream.startTLSPolicy = .required
}
fileprivate func configureXMPPElements() {
//Autoping
xmppAutoPing = XMPPAutoPing(dispatchQueue: DispatchQueue.main)
xmppAutoPing?.activate(xmppStream)
xmppAutoPing?.addDelegate(self, delegateQueue: DispatchQueue.main)
xmppAutoPing?.pingInterval = 2
xmppAutoPing?.pingTimeout = 2
// Reconnect
self.xmppReconnect = XMPPReconnect()
// Storage
let xmppRosterStorage = XMPPRosterCoreDataStorage()
self.xmppRoster = XMPPRoster(rosterStorage: xmppRosterStorage, dispatchQueue: DispatchQueue.main)
self.xmppRoster.autoFetchRoster = true
self.xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = true
self.xmppvCardStorage = XMPPvCardCoreDataStorage.sharedInstance()
self.xmppvCardTempModule = XMPPvCardTempModule(vCardStorage: xmppvCardStorage)
self.xmppvCardAvatarModule = XMPPvCardAvatarModule(vCardTempModule: xmppvCardTempModule)
self.xmppCapabilitiesStorage = XMPPCapabilitiesCoreDataStorage.sharedInstance()
self.xmppCapabilities = XMPPCapabilities(capabilitiesStorage: xmppCapabilitiesStorage)
self.xmppMessageArchivingStorage = XMPPMessageArchivingCoreDataStorage.sharedInstance()
self.xmppMessageArchivingModule = XMPPMessageArchiving(messageArchivingStorage: xmppMessageArchivingStorage)
self.xmppMessageArchivingModule.clientSideMessageArchivingOnly = false
self.xmppMessageArchivingModule.activate(self.xmppStream)
self.xmppMessageArchivingModule.addDelegate(self, delegateQueue: DispatchQueue.main)
//Activate xmpp modules
self.xmppReconnect.activate(self.xmppStream)
self.xmppRoster.activate(self.xmppStream)
self.xmppvCardTempModule.activate(self.xmppStream)
self.xmppvCardAvatarModule.activate(self.xmppStream)
self.xmppCapabilities.activate(self.xmppStream)
// Add ourself as a delegate to anything we may be interested in
self.xmppRoster.addDelegate(self, delegateQueue: DispatchQueue.main)
}
func loginWithId(_ userId: String, password: String) {
if self.xmppStream == nil {
establishConnection()
}
self.userId = userId
self.password = password
xmppStream.myJID = XMPPJID(string: userId)
do {
try xmppStream?.connect(withTimeout: XMPPStreamTimeoutNone)
} catch {
print("connection failed")
}
}
fileprivate func authentictae() {
do {
try self.xmppStream.authenticate(withPassword: password)
}
catch {
print("not authenticate")
}
}
// Delegate Methods
func xmppStream(_ sender: XMPPStream!, socketDidConnect socket: GCDAsyncSocket!) {
print("socketDidConnect:")
sender.enableBackgroundingOnSocket = true
}
func xmppStreamDidStartNegotiation(_ sender: XMPPStream!) {
print("xmppStreamDidStartNegotiation:")
}
func xmppStreamDidConnect(_ sender: XMPPStream!) {
authentictae()
print("Stream: Connected")
}
func xmppStreamDidAuthenticate(_ sender: XMPPStream!) {
print("Stream: Authenticated")
}