【问题标题】:flutter/dart connect to Azure via mqttsflutter/dart 通过 mqtts 连接到 Azure
【发布时间】:2021-11-16 06:28:21
【问题描述】:

我正在尝试连接到 Azure 上的 MQTT 服务器。

我使用 MQTTBox 作为我们的测试平台,它可以成功连接到 协议:mqtts 主机:.azure-devices.net/$iothub/websocket 用户:.azure-devices.net/testdevice/?api-version=2018-06-30 密码:'SharedAccessSignature sr=.azure-devices.net%2Fdevices%2Ftestdevice&sig=xxxxxxx'

我尝试了 mqtt_client 库,这是服务器端点中“$”符号的问题,并抛出“”SocketException:主机查找失败:'mqttQueue.azure-devices.net/$iothub/websocket'(操作系统错误:没有与主机名关联的地址,errno = 7)"

final client = MqttServerClient('<hubname>.azure-devices.net/\$iothub/websocket', '');
client.port = 8883;
client.secure = true;

final connMess = MqttConnectMessage()
      .authenticateAs('<hubname>.azure-devices.net/testdevice/?api-version=2018-06-30',
      'SharedAccessSignature sr=<hubname>.azure-devices.net%2Fdevices%2Ftestdevice&sig=xxxxxxx')
      .withClientIdentifier('testdevice')
      .withWillTopic('devices/testdevice/messages/events/') // If you set this you must set a will message
      .withWillMessage('My Will message')
      .startClean() // Non persistent session for testing
      .withWillQos(MqttQos.atLeastOnce);

client.connectionMessage = connMess;

try {
  await client.connect();
}

我也试过了,没成功

final client = MqttServerClient('HostName=<hubname>.azure-devices.net;DeviceId=testDevice;SharedAccessKey=m....Y=','')

感谢 dart/flutter 中 Azure 的任何工作示例,因为我无法将 Azure 给定参数映射到库中的参数。

【问题讨论】:

  • 我怀疑您需要按照example 传递一个URL(例如MqttServerClient('wss://&lt;hubname&gt;.azure-devices.net/\$iothub/websocke', ''))。
  • 这仅适用于 Web 套接字 :-(。对于开发人员来说,对于 mqtts,主机名应该只是为 nslookup 准备的主机名,而不是带有协议前缀或任何路径的完整 URL。
  • 我假设您使用的是 websockets(链接确实以 /websocket... 结尾)。如果您不使用 websockets,那么包含路径是没有意义的(所以是的,您需要提供原始主机名/IP 和端口)。
  • 终于找到了答案:a) MqttServerClient('mqttQueue.azure-devices.net', '') 没有任何你在 MQTTbox 和其他测试平台中需要的路径/协议 b) .authenticateAs(' .azure-devices.net/testdevice, ,...) 没有协议,记录不正确。

标签: flutter dart mqtt azure-iot-hub


【解决方案1】:
final client = MqttServerClient('<hubname>.azure-devices.net', '');
client.useWebSocket = false;
client.port = 8883;
client.autoReconnect = true;
client.keepAlivePeriod = 3600;

final String user = '<hubname>.azure-devices.net/<your device id>';
late String password; // <== password string is obtained elsewhere
final connMess = MqttConnectMessage()
      .withClientIdentifier(clientIdentifier)
      .startClean();
client.connectionMessage = connMess; 
client.connect(username, password);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-05
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 2020-07-30
    相关资源
    最近更新 更多