在swift 3中无法连接到xmpp中的套接字

问题描述 投票:2回答:1

我试图连接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个回答
0
投票

使该类的单例如下:

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();
    }
© www.soinside.com 2019 - 2024. All rights reserved.