【问题标题】:Can not connect to socket in xmpp in swift 3在swift 3中无法连接到xmpp中的套接字
【发布时间】:2017-01-17 02:57:28
【问题描述】:

我正在尝试连接 xmpp 流套接字但无法连接。我正在使用 swift 3。它在 Objcetiv-C 和 swift 2.0 中工作,但在 swift 3 中不工作。请查看我的以下代码并请帮助我。谢谢。

func setupStream()
{
    xmppStreamChat = XMPPStream.init()

    xmppReconnect = XMPPReconnect()
    //xmppRosterStorage = XMPPRosterCoreDataStorage.init()
    xmppRoster = XMPPRoster.init(rosterStorage: xmppRosterStorage)
    xmppRoster.autoFetchRoster = true;

    xmppStreamChat.addDelegate(self, delegateQueue: DispatchQueue.main)
    xmppRoster.addDelegate(self, delegateQueue: DispatchQueue.main)

    xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = true;

    xmppvCardStorage = XMPPvCardCoreDataStorage.sharedInstance();
    xmppvCardTempModule = XMPPvCardTempModule.init(vCardStorage: xmppvCardStorage)

    xmppvCardAvatarModule = XMPPvCardAvatarModule.init(vCardTempModule: xmppvCardTempModule)
    xmppCapabilitiesStorage = XMPPCapabilitiesCoreDataStorage.sharedInstance();
    xmppCapabilities = XMPPCapabilities.init(capabilitiesStorage: xmppCapabilitiesStorage);
    xmppCapabilities.autoFetchHashedCapabilities = true;
    xmppCapabilities.autoFetchNonHashedCapabilities = false;
    xmppReconnect.activate(xmppStreamChat)
    xmppRoster.activate(xmppStreamChat)

    xmppvCardTempModule.activate(xmppStreamChat)
    xmppvCardAvatarModule.activate(xmppStreamChat)
    xmppCapabilities.activate(xmppStreamChat)


    xmppStreamChat.hostName = "95.138.180.254"
    xmppStreamChat.hostPort = 5222
}

func connect() -> Bool
{
    if !((xmppStreamChat.isConnected()))
    {
        let jabberID = (UserDefaults.standard.value(forKey: "XMPPUserName") as! String) + "@95.138.180.254"
        let myPassword = UserDefaults.standard.string(forKey: "XMPPPassword")

        if !((xmppStreamChat.isDisconnected()))
        {
            return true
        }

        if jabberID.length == 0 && myPassword?.length == 0
        {
            return false
        }
        xmppStreamChat.myJID = XMPPJID.init(string: jabberID)
        do {
            try xmppStreamChat.connect(withTimeout: XMPPStreamTimeoutNone)
            print(xmppStreamChat.isConnecting()) // This prints true
            //try xmppStreamChat.connect(to: xmppStreamChat.myJID, withAddress: nil, withTimeout: XMPPStreamTimeoutNone)
            print("Connection success")
            return true
        } catch {
            print("Something went wrong!")
            return false
        }
    }
    else
    {
        return true
    }
}

xmppStream(_sender: XMPPStream, socketDidConnect socket: GCDAsyncSocket) 该方法被调用,但之后没有任何委托方法被调用。如果可以的话请帮忙。提前致谢。

【问题讨论】:

  • 你找到解决办法了吗?

标签: swift xmpp


【解决方案1】:

按如下方式制作该类的单例:

class XMPPController: NSObject {

static let sharedInstance = XMPPController();

你的设置方法:

    func setupStream()
    {
    /***set up code goes here***/
    }

现在你的连接方法:

func connect() -> Bool{
    /***connect code goes here***/
    }

现在在另一个控制器中调用它,如下所示:

    func testConnect() {
            XMPPController.sharedInstance.setupStream();
            XMPPController.sharedInstance.connect();
    }

【讨论】:

    猜你喜欢
    • 2017-04-06
    • 2018-03-12
    • 2011-10-03
    • 1970-01-01
    • 2018-02-26
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多