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

MQTT客户端收0数据后断开,自签名证书场景排查求助

CocoaMQTT客户端发送数据失败后被Broker断开连接排查

问题场景

我的CocoaMQTT客户端在尝试向使用自签名证书的Broker发送数据时,出现发送0字节后连接被断开的情况。Android端使用PAHO MQTT客户端连接该Broker一切正常,需要判断是代码问题还是Broker问题。

客户端代码

func selfSignedSSLSetting() {
    if mqttVesion == "3.1.1" {

        let clientID = "CocoaMQTT-\(animal!)-"
        mqtt = CocoaMQTT(clientID: clientID, host: defaultHost, port: 1883)
        mqtt!.username = "xxx"
        mqtt!.password = "xxx"
        mqtt!.keepAlive = 60
        mqtt!.delegate = self
        mqtt!.allowUntrustCACertificate = true
        mqtt!.logLevel = .debug

    }else if mqttVesion == "5.0" {

        let clientID = "CocoaMQTT5-\(animal!)-"
        mqtt5 = CocoaMQTT5(clientID: clientID, host: defaultHost, port: 1883)

        let connectProperties = MqttConnectProperties()
        connectProperties.topicAliasMaximum = 0
        connectProperties.sessionExpiryInterval = 0
        connectProperties.receiveMaximum = 100
        connectProperties.maximumPacketSize = 500

        mqtt5!.connectProperties = connectProperties
        mqtt5!.logLevel = .debug

        mqtt5!.username = "xxx"
        mqtt5!.password = "xxx"
        mqtt5!.keepAlive = 60
        mqtt5!.delegate = self

        mqtt5!.allowUntrustCACertificate = true

    }
}

连接日志

[TRACE] [didStateChangeTo]: new state: connecting
CocoaMQTT(info): Connected to xx.xx.xx.xxx : 1883
CocoaMQTT(info): Enable backgrounding socket successfully
CocoaMQTT(debug): SEND: CONNECT(id: CocoaMQTT-Sheep-, username: xxx, password: xxx, keepAlive : 60, cleansess: true)
CocoaMQTT(debug): =========================MQTT 3.1.1=========================
CocoaMQTT(debug): packetFixedHeaderType 16
CocoaMQTT(debug): remainingLen(len: len) [68]
CocoaMQTT(debug): variableHeader something
CocoaMQTT(debug): payload something
CocoaMQTT(debug): =============================================================
CocoaMQTT(debug): socket wrote data 0
CocoaMQTT(debug): socket disconnected
[TRACE] [didDisconnect]: Error Domain=NSPOSIXErrorDomain Code=54 "Connection reset by peer" UserInfo={NSLocalizedDescription=Connection reset by peer, NSLocalizedFailureReason=Error in read() function}
[TRACE] [didStateChangeTo]: new state: disconnected

问题分析与解决方案

从日志和Android端的对比来看,Broker本身没有问题,问题出在iOS客户端的配置上,具体排查点如下:

1. 端口与SSL配置不匹配

你设置了allowUntrustCACertificate = true(针对SSL/TLS加密连接),但使用的是1883端口(MQTT非加密默认端口)。如果Broker要求加密连接,应该使用8883端口,同时需要明确启用SSL:

// MQTT 3.1.1
mqtt!.enableSSL = true
mqtt!.port = 8883

// MQTT 5.0
mqtt5!.enableSSL = true
mqtt5!.port = 8883

Android端大概率正确使用了8883加密端口,因此连接正常。

2. ClientID格式问题

你的ClientID末尾带有横杠,部分Broker对ClientID的格式有严格要求(比如不能以特殊字符结尾),可以尝试修改为更简洁的格式测试:

let clientID = "CocoaMQTT-\(animal!)"

3. MQTT 5.0连接属性冲突

你设置了topicAliasMaximum = 0,部分Broker可能不支持该值,尝试注释掉该属性或设置为默认值:

// connectProperties.topicAliasMaximum = 0

4. 验证顺序

优先尝试修改端口并启用SSL,这是最可能解决问题的点,因为日志中的Connection reset by peer通常是由于协议不匹配(明文连接到加密端口,或加密连接到明文端口)导致的。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 11:57:13