我正在使用 CocoaMQTT 并尝试使用以下代码连接到 MQTT 服务:
let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
//This is myHost: wss://myserver.test:9001
let mqtt = CocoaMQTT(clie.ntID: clientID, host: "myserver.test", port: 9001)
mqtt.keepAlive = 10000
mqtt.username = "username"
mqtt.password = "password"
mqtt.enableSSL = true
mqtt.delegate = self
mqtt.connect()
mqtt.autoReconnect = true
我收到错误:Error Domain=GCDAsyncSocketErrorDomain Code=7“套接字被远程对等点关闭” UserInfo={NSLocalizedDescription=套接字被远程对等点关闭}
CocoaMQTT 支持 wss 还是只支持 tcp?如果不支持,还有其他swift客户端吗?
最佳答案
这是 CocoaMQTT 中的示例。也许有帮助。
let clientID = "CocoaMQTT-\(animal!)-" + String(ProcessInfo().processIdentifier)
mqtt = CocoaMQTT(clientID: clientID, host: "127.0.0.1", port: 8883)
mqtt!.username = ""
mqtt!.password = ""
mqtt!.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
mqtt!.keepAlive = 60
mqtt!.delegate = self
mqtt!.enableSSL = true
let clientCertArray = getClientCertFromP12File(certName: "client-keycert", certPassword: "MySecretPassword")
var sslSettings: [String: NSObject] = [:]
sslSettings[GCDAsyncSocketManuallyEvaluateTrust as String] = NSNumber(value: true)
sslSettings[kCFStreamSSLCertificates as String] = clientCertArray
mqtt!.sslSettings = sslSettings
关于swift - 如何使用 swift 通过 wss 连接到 MQTT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47699595/