首页 > 解决方案 > 使用 QoS.EXACTLY_ONCE 订阅主题时使用 awsiotsdk,遇到 AWS_ERROR_MQTT_UNEXPECTED_HANGUP

问题描述

我正在关注pub/sub中的示例awsiotsdk

# boilerplate setup code omitted
self.mqtt_connection = awsiot.mqtt_connection_builder.websockets_with_default_aws_signing(...)
connect_future = self.mqtt_connection.connect()
print('Connected: ', connect_future.result())
[OUTPUT] Connected:  {'session_present': True}

连接过程中的一切看起来都很好。接下来我订阅一个主题:

def mycallback(*args, **kwargs):
  print("mycallback: ", args, kwargs)

subscribe_future, packet_id = self.mqtt_connection.subscribe(
  topic='test/topic',
  callback=mycallback,
  qos=awscrt.mqtt.QoS.AT_LEAST_ONCE
)
subscribe_future.result()

上面的代码有效,我以后可以成功发布和接收消息。

但是,如果我将 QoS 更改为,则会EXACTLY_ONCE出现以下错误:

In _on_connection_interrupted: (), {'connection': <awscrt.mqtt.Connection object at 0x7f804acfea40>, 'error': AwsCrtError(name='AWS_ERROR_MQTT_UNEXPECTED_HANGUP', message='The connection was closed unexpectedly.', code=5134)}
In _on_connection_resumed: (), {'connection': <awscrt.mqtt.Connection object at 0x7f804acfea40>, 'return_code': <ConnectReturnCode.ACCEPTED: 0>, 'session_present': True}
In _on_connection_interrupted: (), {'connection': <awscrt.mqtt.Connection object at 0x7f804acfea40>, 'error': AwsCrtError(name='AWS_ERROR_MQTT_UNEXPECTED_HANGUP', message='The connection was closed unexpectedly.', code=5134)}
In _on_connection_resumed: (), {'connection': <awscrt.mqtt.Connection object at 0x7f804acfea40>, 'return_code': <ConnectReturnCode.ACCEPTED: 0>, 'session_present': True}
<repeats a few more lines>

EXACTLY_ONCE 有什么问题?这似乎是我想要的 QoS。

标签: amazon-web-servicesmqttaws-iot

解决方案


EXACTLY_ONCEAWS IoT Core 服务器当前不支持(MQTT QOS 级别 2)。源代码中提到了这一点:

QoS 2 - Exactly once delivery
This is the highest quality of service, for use when neither loss nor
duplication of messages are acceptable. There is an increased overhead
associated with this quality of service.

Note that, while this client supports QoS 2, the AWS IoT Core server
does not support QoS 2 at time of writing (May 2020).

并在开发者指南中:

MQTT 协议定义了第三级 QoS,即 2 级,但 AWS IoT 不支持它。


推荐阅读