如何解决:Node.js Server中的onclosenexttick提前关闭? [英] How to solve: premature close at onclosenexttick in Node.js Server?

查看:108
本文介绍了如何解决:Node.js Server中的onclosenexttick提前关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决此错误,我正在使用AWS IoT运行Node.js,然后有时会显示此错误:

How to solve this error, I am running my Node.js with AWS IoT then it at times shows this error:

      throw er; // Unhandled 'error' event
      ^

Error: How  (/home/ec2-user/work/nodejs_27_01/node_modules/end-of-stream/index.js:54:                                                                                                     86)
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
Emitted 'error' event on DeviceClient instance at:
    at MqttClient.<anonymous> (/home/ec2-user/work/nodejs_27_01/node_modules/aws-iot-device-sdk/                                                                                                     device/index.js:772:15)
    at MqttClient.emit (events.js:333:22)
    at MqttClient.EventEmitter.emit (domain.js:485:12)
    at TLSSocket.f (/home/ec2-user/work/nodejs_27_01/node_modules/once/once.js:25:25)
    at onclosenexttick (/home/ec2-user/work/nodejs_27_01/node_modules/end-of-stream/index.js:54:                                                                                                     73)

推荐答案

可能有多种原因:

clientId一次只能用于一个连接.如果在建立另一个连接时使用相同的clientId连接,则旧的连接将被丢弃(这会导致过早关闭错误),并且新的连接也将建立.

The clientId can only be used for one connection at a time. If you connect with the same clientId while another connection is established, the older connection gets dropped (which leads to the premature close error) and the new connection is established.

客户端正在使用已使用的客户端ID.在这种情况下,已经连接的客户端将被断开.(来源)

权限

如果设备(来自 aws的mqtt.Client-iot-device-sdk-js )没有正确的权限来连接和/或发布/订阅/接收给定主题上的消息.

Permissions

This error can happen if a device (mqtt.Client from aws-iot-device-sdk-js) does not hold the correct permissions to connect and/or publish/subscribe/receive messages on a given topic.

有关更多文档,请参见此处: https://docs.aws.amazon.com/iot/latest/developerguide/pub-sub-policy.html

See here for more documentation: https://docs.aws.amazon.com/iot/latest/developerguide/pub-sub-policy.html

该策略应如下所示(示例显示了

The policy should look like this (example shows a Cloudformation Iot Policy resource):

MyIotThingsPolicy:
  Type: AWS::IoT::Policy
  Properties:
    PolicyDocument:
      Version: "2012-10-17"
      Statement:
        - Action: iot:Connect
          Effect: Allow
          Resource: !Join [ "", [!Sub "arn:aws:iot:${AWS::Region}:${AWS::AccountId}:client/",
                                 "${iot:ClientId}"] ]
        - Action: iot:Receive
          Effect: Allow
          Resource: !Join [ "", [!Sub "arn:aws:iot:${AWS::Region}:${AWS::AccountId}:topic/",
                                "${iot:ClientId}/eg/your/broadcast/topic"] ]
        - Action: iot:Subscribe
          Effect: Allow
          Resource: !Join [ "", [!Sub "arn:aws:iot:${AWS::Region}:${AWS::AccountId}:topicfilter/",
                                 "${iot:ClientId}/eg/your/broadcast/topic"] ]
        - Action: iot:Publish
          Effect: Allow
          Resource: !Join [ "", [!Sub "arn:aws:iot:${AWS::Region}:${AWS::AccountId}:topic/",
                                 "${iot:ClientId}/eg/your/publish/topic"] ]

!Join 是必需的,因为Cloudformation会尝试解析 $ {iot:ClientId} ,这是运行时值,在部署过程中未知.

The !Join is necessary since Cloudformation would try to resolve ${iot:ClientId}, which is a runtime value, and not known during deployment.

  • 正如 Sandeep Patel 所建议的那样,如果您要采取行动,则应该实施错误回调客户端这种情况:

  • As Sandeep Patel also suggests, you should implement an error callback if you want to act on this situation on client side:

device.on('error', (error) => {
  // error.message might be 'premature close'
});

  • 您还可以在主题 $ aws/events/presence/disconnected/< clientId> 上查找 disconnectReason ,请参见: 查看全文

  • 登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆