之前用Android原生可以连阿里云没问题,这边也都是走通的,但是到flutter怎么都连不上,那就解决问题呗,一步一步来:

下载了一个Mqtt.fx把参数都输进去,确保可以连接成功,那真的可能就是mqtt_client这个库的问题了

各种配置检查了好几遍,确定没问题了,但是就是连不通

设置了

client.secure = true;

报错:

flutter: Exception: HandshakeException: Connection terminated during handshake

后来看了下我们没做认证,应该把这个secure设为false;

报错:

flutter: Exception: mqtt-client::NoConnectionException: The maximum allowed connection attempts ({3}) were exceeded. The broker is not responding to the connection request message (Missing Connection Acknowledgement?

这个问题很广泛,google了半天,也没解决了

mqtt_client的github的issues翻了个底朝天,终于解决了

https://github.com/shamblett/mqtt_client/issues/126

// 新建MqttConnectMessage的时候添加
.withProtocolName("MQTT")
.withProtocolVersion(4)

final connMessage = MqttConnectMessage()
        .withClientIdentifier(clientId)
        .authenticateAs(userName, password)
        .keepAliveFor(90)
        .withWillTopic('willtopic')
        .withWillMessage('Will message')
        .startClean()
        .withProtocolName("MQTT")
        .withProtocolVersion(4)
        .withWillQos(MqttQos.atLeastOnce);

client.connectionMessage = connMessage;        

阿里的官方文档没有flutter的,mqtt这个也么有说要加这个的,一个小问题卡了一下午,记录一下,能帮你们节省时间的话点个赞就行

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐