【问题标题】:XMPP connection issue on IOS using Swift使用 Swift 的 IOS 上的 XMPP 连接问题
【发布时间】:2015-01-25 02:12:40
【问题描述】:

我正在尝试使用 swift 使用 XMPP 框架(https://github.com/robbiehanson/XMPPFramework)。 我是 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.
    }
}

用户名和密码以及服务器详细信息是正确的,因为我使用 adium 连接到服务器并且它工作正常。

【问题讨论】:

  • 我也有同样的问题。你有什么解决办法吗?

标签: ios swift xmppframework


【解决方案1】:

请记住,建立连接需要几秒钟。 使用其他委托方法来跟踪连接的状态。

【讨论】:

    【解决方案2】:

    设置您的主机名和端口。

    调用方法如下所述。

    • 配置XMPP()
    • 配置XMPPElements()
    • loginWithId("userId", 密码:"password")

    之后,将调用委托方法并完成身份验证。

    身份验证后,必须发送出席信息。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 2014-02-06
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 2014-02-28
      • 2013-09-24
      • 1970-01-01
      相关资源
      最近更新 更多