Javascript AWS物联网核心-如何在连接丢失后启用无限重连接(MQTT)

Javascript AWS物联网核心-如何在连接丢失后启用无限重连接(MQTT),javascript,node.js,amazon-web-services,raspberry-pi,aws-iot,Javascript,Node.js,Amazon Web Services,Raspberry Pi,Aws Iot,我主要在Raspberry PI上使用aws-iot-device-sdk-js-v2,并在Windows 10上进行测试。 我是这样连接到AWS的: const client_bootstrap = new io.ClientBootstrap(); let config_builder = iot.AwsIotMqttConnectionConfigBuilder.new_mtls_builder_from_path(config.aws.cert_path, config.aws.key

我主要在Raspberry PI上使用aws-iot-device-sdk-js-v2,并在Windows 10上进行测试。 我是这样连接到AWS的:

const client_bootstrap = new io.ClientBootstrap();

let config_builder = iot.AwsIotMqttConnectionConfigBuilder.new_mtls_builder_from_path(config.aws.cert_path, config.aws.key_path);

config_builder.with_certificate_authority_from_path(undefined, config.aws.root_ca_path);

config_builder.with_clean_session(false);
config_builder.with_client_id(config.aws.thing_name);
config_builder.with_endpoint(config.aws.iot_endpoint);

const mqttConfig = config_builder.build();

const client = new mqtt.MqttClient(client_bootstrap);
connection = client.new_connection(mqttConfig);

shadow = new iotshadow.IotShadowClient(connection);

thingName = config.aws.thing_name;

return connection.connect();
当存在连接时,一切都会完美工作。如果我断开与internet的连接,经过一段时间(20-30秒),我将获得
中断
事件,并出现以下错误:


    CrtError: aws-c-io: AWS_IO_SOCKET_CLOSED, socket is closed.
        at MqttClientConnection._on_connection_interrupted (/home/mstupar/device/node_modules/aws-crt/dist/native/mqtt.js:334:32)
        at /home/mstupar/device/node_modules/aws-crt/dist/native/mqtt.js:114:113 {
      error: 1051,
      error_code: 1051,
      error_name: 'AWS_IO_SOCKET_CLOSED'
    }

如果我在此中断后连接到internet,我将得到一个未处理的异常,其堆栈跟踪如下:

################################################################################
Resolved stacktrace:
################################################################################
0x00007fc3193b781b: ?? ??:0
0x00000000000b1383: s_print_stack_trace at module.c:?
0x00000000000128a0: __restore_rt at ??:?
0x00007fc31c4bef47: ?? ??:0
0x00007fc31c4c08b1: ?? ??:0
node() [0x95c589]
node(napi_acquire_threadsafe_function+0x27) [0x9cf1e7]
0x00007fc3192f1931: ?? ??:0
0x00000000000b4306: s_on_connection_resumed at mqtt_client_connection.c:?
0x00000000000de34c: s_packet_handler_connack at client_channel_handler.c:?
0x00000000000dee9e: s_process_read_message at client_channel_handler.c:?
0x000000000011269d: s_s2n_handler_process_read_message at s2n_tls_channel_handler.c:?
0x0000000000113bbe: s_do_read at socket_channel_handler.c:?
0x00000000001141f2: s_on_readable_notification at socket_channel_handler.c:?
0x0000000000110a5a: s_on_socket_io_event at socket.c:?
0x000000000010bb9a: s_main_loop at epoll_event_loop.c:?
0x0000000000177c1b: thread_fn at thread.c:?
0x00000000000076db: start_thread at ??:?
0x00007fc31c5a1a3f: ?? ??:0
################################################################################
Raw stacktrace:
################################################################################
/home/mstupar/device/node_modules/aws-crt/dist/bin/linux-x64/aws-crt-nodejs.node(aws_backtrace_print+0x4b) [0x7fc3193b781b]
/home/mstupar/device/node_modules/aws-crt/dist/bin/linux-x64/aws-crt-nodejs.node(+0xb1383) [0x7fc3192f1383]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x128a0) [0x7fc31c8928a0]
/lib/x86_64-linux-gnu/libc.so.6(gsignal+0xc7) [0x7fc31c4bef47]
/lib/x86_64-linux-gnu/libc.so.6(abort+0x141) [0x7fc31c4c08b1]
node() [0x95c589]
node(napi_acquire_threadsafe_function+0x27) [0x9cf1e7]
/home/mstupar/device/node_modules/aws-crt/dist/bin/linux-x64/aws-crt-nodejs.node(aws_napi_queue_threadsafe_function+0x11) [0x7fc3192f1931]
/home/mstupar/device/node_modules/aws-crt/dist/bin/linux-x64/aws-crt-nodejs.node(+0xb4306) [0x7fc3192f4306]
/home/mstupar/device/node_modules/aws-crt/dist/bin/linux-x64/aws-crt-nodejs.node(+0xde34c) [0x7fc31931e34c]
/home/mstupar/device/node_modules/aws-crt/dist/bin/linux-x64/aws-crt-nodejs.node(+0xdee9e) [0x7fc31931ee9e]
/home/mstupar/device/node_modules/aws-crt/dist/bin/linux-x64/aws-crt-nodejs.node(+0x11269d) [0x7fc31935269d]
/home/mstupar/device/node_modules/aws-crt/dist/bin/linux-x64/aws-crt-nodejs.node(+0x113bbe) [0x7fc319353bbe]
/home/mstupar/device/node_modules/aws-crt/dist/bin/linux-x64/aws-crt-nodejs.node(+0x1141f2) [0x7fc3193541f2]
/home/mstupar/device/node_modules/aws-crt/dist/bin/linux-x64/aws-crt-nodejs.node(+0x110a5a) [0x7fc319350a5a]
/home/mstupar/device/node_modules/aws-crt/dist/bin/linux-x64/aws-crt-nodejs.node(+0x10bb9a) [0x7fc31934bb9a]
/home/mstupar/device/node_modules/aws-crt/dist/bin/linux-x64/aws-crt-nodejs.node(+0x177c1b) [0x7fc3193b7c1b]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x76db) [0x7fc31c8876db]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x3f) [0x7fc31c5a1a3f]
是否有可能避免AWS_IO_插槽_通过附加配置关闭? 我应该如何正确地重新连接?有没有像socket.io那样的自动方式。 设备应能够在不重新启动的情况下至少运行1天。在那一天,可能会发生多个连接中断。对于这种情况,使用systemd重新启动进程不是一个好的解决方案,因为我正在同一进程中运行一些其他模块,这些模块不应受到连接丢失的影响

MqttClientConnection是否需要任何其他配置? 我应该从中断调用一些函数来手动恢复连接吗


提前感谢

此问题已在最新的aws-iot-device-sdk-js-v2 API版本中修复。此问题已在最新的aws-iot-device-sdk-js-v2 API版本中修复。