我正在使用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
我收到错误消息:错误域= GCDAsyncSocketErrorDomain代码= 7“套接字被远程对等方关闭” UserInfo = {NSLocalizedDescription =套接字被远程对等方关闭}
CocoaMQTT是否支持wss或仅支持tcp?如果不支持,是否还有其他客户端需要快速处理?
最佳答案
这是来自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