0

我正在快速创建框架,使用cocoapods我添加了XMPP 框架 ,但不知何故无法连接到我的主机

我的设置:

class XMPPController: NSObject ,XMPPStreamDelegate{

 var XMPP_HOST = "**************";
 var userJid:XMPPJID = XMPPJID();
 var password = "";

var xmppStream:XMPPStream;

init(jid: String, password: String) {


if let userjabberid = XMPPJID(string: jid) {
    self.userJid = userjabberid;
}
self.password = password;

self.xmppStream = XMPPStream();
self.xmppStream.hostName = XMPP_HOST;
self.xmppStream.hostPort = 5222;
self.xmppStream.startTLSPolicy = XMPPStreamStartTLSPolicy.allowed;
self.xmppStream.myJID = self.userJid;

super.init();
self.xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)
}

和我的连接方法:

func connect() {
if !self.xmppStream.isDisconnected {
    return
}

do {
    try self.xmppStream.connect(withTimeout: XMPPStreamTimeoutNone);
} catch let err {
    print(err);
}

}

和我的委托方法:

func xmppStreamWillConnect(_ sender: XMPPStream) {
    print("will connect");
}
func xmppStream(_ sender: XMPPStream, socketDidConnect socket: GCDAsyncSocket) {
    print("socket")
}
func xmppStreamDidStartNegotiation(_ sender: XMPPStream) {
    print("negotiate")
}
func xmppStream(_ sender: XMPPStream, didReceiveError error: DDXMLElement) {
    print(error);
}
func xmppStreamDidDisconnect(_ sender: XMPPStream, withError error: Error?) {
    print("disconnected");
}
func xmppStreamDidConnect(_ sender: XMPPStream) {
    print("connected");
    try! sender.authenticate(withPassword: self.password);
}
func xmppStreamDidAuthenticate(_ sender: XMPPStream) {
    print("authenticated");
}
func xmppStream(_ sender: XMPPStream, didNotAuthenticate error: DDXMLElement) {
    print("Stream: Fail to Authenticate");
}

在这里,只xmppStreamWillConnect调用而不调用所有其他委托方法。

4

3 回答 3

0

通过使我的班级单身成为休闲来解决:

static let sharedInstance = XMPPController();

并将其称为:

XMPPController.sharedInstance.connect(Withjid: "***@dev.****.com", Andpassword: "password");
于 2018-10-12T09:48:31.910 回答
0

我也有同样的问题,后来发现我没有启动 mongooseIM 服务器。

从这里下载

第一步:启动服务器

mongooseimctl start

第二步:查看状态

mongooseimctl status

Step3:创建/注册用户

mongooseimctl register itsyourusername localhost itsapassword

第 4 步:在 XMPP 客户端框架中使用这些凭据。

官方文档

于 2019-12-16T16:42:44.233 回答
0

您需要XMPPController像下面这样初始化并调用连接函数:

self.xmppController = XMPPController(jid: String, password: String)
self.xmppController.xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)
self.xmppController.connect()
于 2018-10-10T11:39:49.850 回答