Flutter Mqtt连接阿里云失败

之前用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这个也么有说要加这个的,一个小问题卡了一下午,记录一下,能帮你们节省时间的话点个赞就行

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值