【问题标题】:Flutter setup MQTT with .p12 fileFlutter 使用 .p12 文件设置 MQTT
【发布时间】:2022-12-12 09:29:05
【问题描述】:

我目前正在使用 Flutter 重建一个应用程序,在旧的 iOS 和 Android 应用程序中,两者都会从后端获取 .p12 文件以设置 MQTT 套接字连接。

但是我尝试使用mqtt_client的Flutter包似乎需要useCertificateChainusePrivateKeysetClientAuthorities 三个这样的文件:

SecurityContext context = new SecurityContext()
  ..useCertificateChain('path/to/my_cert.pem')
  ..usePrivateKey('path/to/my_key.pem', password: 'key_password')
  ..setClientAuthorities('path/to/client.crt', password: 'password');
client.secure = true;
client.securityContext = context;

代码来自:https://emqx.medium.com/using-mqtt-in-the-flutter-project-6a5d90179c8b

我一直在研究mqtt_client包,但是他们提供的示例和文档似乎没有使用.p12文件建立套接字连接的选项。

如果我必须将 .p12 文件下载到手机上,然后再次提取并重新保存三个文件,那么使用 Flutter 就没有意义了。

有什么办法可以在mqtt_client 包中使用.p12 文件,或者是否有任何其他选项或包可以实现这一点?

感谢您的帮助!

【问题讨论】:

    标签: flutter sockets mqtt iot p12


    【解决方案1】:

    我想通了,我可以使用 client.connect 插入 p12 文件,但是包文档对此没有任何说明。这应该是建立 MQTT 连接的一种非常常见的方式。

    final client = MqttClient('[mqtt.example.com](http://mqtt.example.com/)', '');
    
    client.connect('path/to/file.p12', 'p12-file-password')
    .then((result) {
    print('Connected to MQTT broker');
    })
    .catchError((error) {
    print('Failed to connect to MQTT broker: $error');
    });
    

    【讨论】:

      猜你喜欢
      • 2022-10-22
      • 2016-09-30
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      相关资源
      最近更新 更多