简体   繁体   English

使用Swift的IOS上的XMPP连接问题

[英]XMPP connection issue on IOS using Swift

I am trying to use the XMPP framework( https://github.com/robbiehanson/XMPPFramework ) using swift. 我正在尝试通过swift使用XMPP框架( https://github.com/robbiehanson/XMPPFramework )。 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. 用户名,密码和服务器详细信息是正确的,因为我使用adium连接到服务器,并且工作正常。

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() configureXMPP()
  • configureXMPPElements() configureXMPPElements()
  • loginWithId("userId", password: "password") loginWithId(“ userId”,密码:“ 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")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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