简体   繁体   中英

XMPP connection issue on IOS using Swift

I am trying to use the XMPP framework( https://github.com/robbiehanson/XMPPFramework ) using swift. I am new to swift

    class ViewController: UIViewController {
    var password: NSString?
    var isOpen: Bool = false
    var xstream: XMPPStream?

    var loginServer: String = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        println(connect())
        // Do any additional setup after loading the view, typically from a nib.
    }
        override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func connect() ->Bool{


        var xstream = XMPPStream()
        var error: NSError?
        xstream.addDelegate(self, delegateQueue: dispatch_get_main_queue())

        xstream.myJID = XMPPJID.jidWithString("test@localhost")
        xstream.hostName="127.0.0.1"
        xstream.hostPort=5222
        var password = "testing"

        if !xstream.connectWithTimeout(XMPPStreamTimeoutNone, error: &error) {
          println(error)

        }
        println(xstream.isConnecting()) // This prints true
        return xstream.isConnected();// This prints false.
    }
}

The username and password and server details are correct because i use adium to connect to server and it works fine.

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.

  • configureXMPP()
  • configureXMPPElements()
  • loginWithId("userId", password: "password")

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")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM