You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS Swift应用移动数据下MQTT Over WebSocket/SSL连接自动断开问题

Swift iOS应用移动数据下WebSocket/SSL端口连接Cloud-MQTT断开问题

我们有两款iOS应用,一款基于React-Native开发,另一款为Swift原生应用。当前问题:

  • Swift应用使用移动数据通过WebSocket/SSL端口连接Cloud-MQTT时会自动断开(iPhone与树莓派同网络时连接正常)
  • React-Native应用在所有场景下连接均正常

使用普通MQTT端口时,Swift应用通过移动数据可正常连接,但树莓派登录时提供的连接凭证对应的网关仅开放WebSocket和SSL端口,且每个设备凭证不同,无法设置静态端口。

MQTT Broker及端口信息

MQTT Cloud Broker
Server     driver.cloudmqtt.com
User :      xxxxxx
Password:   xxxxxx
Port:       18658 //仅该端口可用
SSL Port:       28658
Websockets Port (TLS only):   38658
Mosquitto version:       1.5.7 

连接代码

func connect(host: String, port: Int, username: String?, password: String?, cleanSession: Bool) {
        
    guard !host.isEmpty else {
        delegate?.onMqttDisconnected()
        return
    }
    
    status = .connecting
    let clientId = "CONNECTED_GEAR-" + String(ProcessInfo().processIdentifier)
    
    if NetworkMonitor.shared.connection == .wifi {
        mqttClient = CocoaMQTT(clientID: clientId, host: host, port: UInt16(port))
        if let mqttClient = mqttClient {
            UserManager.shared.isConnected = true
            mqttClient.username = username
            mqttClient.password = password
            mqttClient.keepAlive = UInt16(60)
            mqttClient.cleanSession = cleanSession
            mqttClient.backgroundOnSocket = true
            mqttClient.delegate = self
           
           _ = mqttClient.connect()
        } else {
            
            UserManager.shared.isConnected = false
            delegate?.onMqttError(message: "Mqtt initialization error")
            status = .error
        }
    } else {
        
             let socket = CocoaMQTTWebSocket()
           
             socket.enableSSL = false
    
            mqttClient = CocoaMQTT(clientID: clientId, host: host, port: UInt16(port), socket: socket)
            if let mqttClient = mqttClient {
                UserManager.shared.isConnected = true
                mqttClient.username = username
                mqttClient.password = password
                mqttClient.keepAlive = UInt16(60)
                mqttClient.cleanSession = cleanSession
                

                mqttClient.logLevel = .debug
                mqttClient.autoReconnect = false
                mqttClient.allowUntrustCACertificate = true
                mqttClient.backgroundOnSocket = true
                mqttClient.delegate = self

                _ = mqttClient.connect()
            } else {
                
                UserManager.shared.isConnected = false
                delegate?.onMqttError(message: "Mqtt initialization error")
                status = .error
            }
    }
   
}

MQTT日志

设置enableSSL = false时

Network reachable via Cellular 
External Host: driver.cloudmqtt.com
External Port: 38658
External User: xxxxxxx
External Password: xxxxxxx
CocoaMQTT(debug): ping
CocoaMQTT(debug): SEND: PING
CocoaMQTT(debug): =========================MQTT 3.1.1=========================
CocoaMQTT(debug): packetFixedHeaderType 192
CocoaMQTT(debug): remainingLen(len: len) [0]
CocoaMQTT(debug): variableHeader []
CocoaMQTT(debug): payload []
CocoaMQTT(debug): =============================================================
CocoaMQTT(info): Connected to 3.83.156.245 : 38658
CocoaMQTT(info): Enable backgrounding socket successfully
CocoaMQTT(debug): SEND: CONNECT(id: CONNECTED_GEAR-933, username: xxxxxxx, password: xxxxxxx, keepAlive : 60, cleansess: false)
CocoaMQTT(debug): =========================MQTT 3.1.1=========================
CocoaMQTT(debug): packetFixedHeaderType 16
CocoaMQTT(debug): remainingLen(len: len) [54]
CocoaMQTT(debug): variableHeader [0, 4, 77, 81, 84, 84, 4, 192, 0, 60]
CocoaMQTT(debug): payload [0, 18, 67, 79, 78, 78, 69, 67, 84, 69, 68, 95, 71, 69, 65, 82, 45, 57, 51, 51, 0, 8, 104, 101, 121, 103, 108, 105, 104, 103, 0, 12, 71, 103, 107, 106, 74, 101, 110, 81, 114, 119, 120, 48]
CocoaMQTT(debug): =============================================================
CocoaMQTT(debug): socket wrote data -192
CocoaMQTT(debug): socket wrote data 0
CocoaMQTT(debug): socket disconnected
Disconnect: Socket closed by remote peer

设置enableSSL = true时

External Host: driver.cloudmqtt.com
External Port: 38658
External User: xxxxxxx
External Password: xxxxxxx
CocoaMQTT(debug): ping
CocoaMQTT(debug): SEND: PING
CocoaMQTT(debug): =========================MQTT 3.1.1=========================
CocoaMQTT(debug): packetFixedHeaderType 192
CocoaMQTT(debug): remainingLen(len: len) [0]
CocoaMQTT(debug): variableHeader []
CocoaMQTT(debug): payload []
CocoaMQTT(debug): =============================================================
CocoaMQTT(info): Connected to 3.83.156.245 : 38658
CocoaMQTT(info): Enable backgrounding socket successfully
CocoaMQTT(debug): socket wrote data -192
CocoaMQTT(debug): socket disconnected
Disconnect: The operation couldn’t be completed. (kCFStreamErrorDomainSSL error -9806.)
CocoaMQTT(info): Try reconnect to server after 1s

CocoaMQTT(info): Connected to 3.83.156.245 : 38658
CocoaMQTT(info): Enable backgrounding socket successfully
CocoaMQTT(debug): Call the SSL/TLS manually validating function

备注:已在info.plist中设置AllowArbitraryLoads = true

补充:由于React-Native的地理围栏问题,我们开发了Swift原生和Kotlin原生应用。我查阅了大量MQTT Over WebSocket和SSL端口相关资料,问题仍未解决。

内容的提问来源于stack exchange,提问作者steveSarsawa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 12:24:59