swift - 如何使用 swift 通过 wss 连接到 MQTT?

标签 swift mqtt

我正在使用 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/

相关文章:

ios - 低延迟输入/输出音频队列

python - 无法使用 Mosquitto/Paho for Python 接收超过 20 条 MQTT 消息

python - 使用 paho 传递 crt/pem

python - Paho 用户 on_message/on_connect 不工作

arrays - 是否可以检查 swift 数组中的所有值是否为真而不是一个一个地循环?

ios - 来自另一个目标的 Swift 伞头

java - IoT 网关和云服务器消息传递

android - 按下后退按钮时应用程序崩溃

swift - 无法在 Swift 中为泛型类创建运算符

ios - 从核心数据中按日期对 TableView 进行排序 - Swift4