swift - xmppStreamDidConnect 没有在“swift 框架”中被调用
问题描述
我正在快速创建框架,使用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
调用而不调用所有其他委托方法。
解决方案
通过使我的班级单身成为休闲来解决:
static let sharedInstance = XMPPController();
并将其称为:
XMPPController.sharedInstance.connect(Withjid: "***@dev.****.com", Andpassword: "password");
推荐阅读
- c++ - 创建没有 new/delete 的模板类型
- javascript - 访问对象
- ios - 如何在 SwiftUI 中基于绑定枚举更改 View 的样式?
- python-3.x - 为什么 librosa.load() 会产生内存问题?
- python - Keras 预测精度与训练精度不匹配
- powershell - Powershell - 仅显示 CSV 中运行脚本后 15 分钟内的行
- javascript - 在带有 Expo 的 React Native 中访问和使用 ImagePicker 的要求
- c# - 如何将 Swagger 3 连接到现有的 JWT 身份验证服务?
- c# - 加载 OBJ 文件时的 OpenGL C# UV 映射/UV 坐标问题
- python - 如何在 Azure 上部署烧瓶应用程序?