Watson IoT & Node.JS - 连续重试和连接失败

Watson IoT & Node.JS - Continuous retry & connection failure

背景:我正在创建一个 Node.js Web 应用程序来模拟设备并将事件发布到在 Watson IoT Platform 中注册的 Node-Red 应用程序和设备。

节点红色应用程序订阅事件并执行自定义逻辑。

作为 Node.js Web 应用程序的一部分,在单击 Web 应用程序中的提交按钮后,我提交了带有文本框值的 post 请求。我正在 app.js 中处理请求并发布事件

问题: 当我第一次提交请求时,它工作得很好。但是从第二次开始,就不断尝试重连,反复发布事件

App.js 代码 --> Post 方法部分

app.post('/status/data', function(req, res) {    console.log("3rd param is " + req.body.eid);      application.connect();   console.log("Successfully connected to our IoT service!");      application.on("connect", function () {         console.log("About to publish data ");        application.publishDeviceEvent(deviceType, deviceId, eventType, eventFormat, eventData);         console.log("published data ");    });     res.writeHead(200, "OK", {'Content-Type': 'text/plain'});   res.end();       }); 

第一次post 请求 - 日志详细信息

Jun 29, 2017 04:12:47.346 PM APP/PROC/WEB/0 3rd param is 111111 Jun 29, 2017 04:13:04.785 PM APP/PROC/WEB/0 [BaseClient:connect] Connecting to IoTF with host : ssl://9z9mtd.messaging.internetofthings.ibmcloud.com:8883 Jun 29, 2017 04:13:04.786 PM APP/PROC/WEB/0 Successfully connected to our IoT service! Jun 29, 2017 04:13:04.814 PM APP/PROC/WEB/0 About to publish data Jun 29, 2017 04:13:04.855 PM APP/PROC/WEB/0 [ApplicationClient:publish] Publish: iot-2/type/CardSim/id/EB27FNW/evt/update/fmt/json, {"d":{"eid":222261,"loc":1234}}, QoS : 0 Jun 29, 2017 04:13:04.855 PM APP/PROC/WEB/0 published data Jun 29, 2017 04:13:04.855 PM APP/PROC/WEB/0 [ApplicationClient:connnect] ApplicationClient Connected Jun 29, 2017 04:13:04.855 PM RTR/1

第二次POst请求 - 日志详细信息

PP/PROC/WEB/0 3rd param is 222222 Jun 29, 2017 04:13:52.891 PM APP/PROC/WEB/0 [BaseClient:connect] Connecting to IoTF with host : ssl://9z9mtd.messaging.internetofthings.ibmcloud.com:8883 Jun 29, 2017 04:13:52.891 PM RTR/0 cardreaderapp.mybluemix.net - [2017-06-29T10:43:52.857+0000] "POST /status/data HTTP/1.1" 200 10 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0" "108.168.250.151:18180" "169.47.199.84:63789" x_forwarded_for:"167.230.96.8" x_forwarded_proto:"https" vcap_request_id:"0801beea-5ab8-434b-403d-6c5cc92e9aad" response_time:0.050970748 app_id:"0cdbfec6-ac2a-4be1-9bab-3b1933ba3c57" app_index:"0" x_global_transaction_id:"1003954655" x_b3_traceid:"920cb5951fe2dee3" x_b3_spanid:"920cb5951fe2dee3" x_b3_parentspanid:"-" Jun 29, 2017 04:13:52.908 PM APP/PROC/WEB/0 Successfully connected to our IoT service! Jun 29, 2017 04:13:52.895 PM APP/PROC/WEB/0 [BaseClient:onClose] Connection was closed. Jun 29, 2017 04:13:52.929 PM APP/PROC/WEB/0 [BaseClient:connect] Iotfclient is offline. Retrying connection Jun 29, 2017 04:13:52.928 PM APP/PROC/WEB/0 [BaseClient:connect] Retry in 3 sec. Count : 1 Jun 29, 2017 04:13:52.928 PM APP/PROC/WEB/0 [ApplicationClient:connnect] ApplicationClient Connected Jun 29, 2017 04:13:52.930 PM APP/PROC/WEB/0 About to publish data Jun 29, 2017 04:13:53.972 PM APP/PROC/WEB/0 [ApplicationClient:publish] Publish: iot-2/type/CardSim/id/EB27FNW/evt/update/fmt/json, {"d":{"eid":222261,"loc":1234}}, QoS : 0

这个过程不断重复。请帮助

对于您的应用程序的每个 POST,您将(重新)连接到 IoTP 并注册一个 on-connect() 回调。

您应该在处理 POST 的函数之外连接并注册任何 on-connect 回调(不是发布 POST 数据的回调!),而不是这样做应用程序初始化。

在POST函数中,您唯一需要做的就是调用publishDeviceEvent()

注意 publishDeviceEvent() 当然是异步的,所以它可能不会在函数 returns 时实际发布。您可以通过传入回调来等待它,然后通过 res.end() 写入 header.

来响应调用者