node.js - 如何检查 Azure IOT 中心发送器是否已停止使用 node.js 和 socket.io

标签 node.js socket.io azure-iot-hub

我有两个程序,一个发送者和一个接收者。发送者向 IOT Hub 上的设备发送一些消息,只要发送者发送这些消息,接收者就会收到这些消息。我正在使用 socket.io 将这些消息广播到连接的客户端。然而,当发送者停止时,接收者也会停止,但发送者发送的最后一条消息将无限广播,直到我关闭接收者或发送者再次启动并发送新消息。最后一条消息将被无限复制和广播。如何检查发送程序是否已停止?

这是发件人程序:

var clientFromConnectionString = require('azure-iot-device-mqtt').clientFromConnectionString;
var Message = require('azure-iot-device').Message;
var connectionString = 'conn_string'
var client = clientFromConnectionString(connectionString);

var avgTemperature = 20;

var printResult = function (err, res) {
    if (err) {
        console.log('send error: ' + err.toString());
        return;
    }
    console.log('send status: ' + res.constructor.name);
};

setInterval(function () {
    var currentTemperature = avgTemperature + (Math.random() * 10) - 2;
    var data = JSON.stringify({
        deviceId: 'test',
        temperature: currentTemperature,
        latitude: 50.286264,
        longitude: 19.104079,
        time: Date.now()
    });
    var message = new Message(data);
    console.log("Sending message: " + message.getData());
    client.sendEvent(message, printResult);
}, 5000);

这是接收者和向客户端广播的socket.io:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var EventHubClient = require('azure-event-hubs').Client;
var connectionString = 'conn_string'

var printError = function (err) {
    console.log(err.message);
};

var result;

var printMessage = function (message) {
    console.log('Message received: ');
    result = JSON.stringify(message.body);
    console.log('message: ' + result);
    /* io.on('connection', function(socket){


     socket.on('chat message', function(msg){

     io.emit('chat message', result);
  }); 
}); */

        console.log('');
};

count =0;

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  console.log('user connected');
  socket.on('chat message', function(msg){

     io.emit('chat message', result);

  }); 
  socket.on('disconnect', function(){
    console.log('user disconnected');
      socket.removeAllListeners('disconnect');
      io.removeAllListeners('connection');
  });
});

var client = EventHubClient.fromConnectionString(connectionString);
client.open()
    .then(client.getPartitionIds.bind(client))
    .then(function (partitionIds) {
        return partitionIds.map(function (partitionId) {
            return client.createReceiver('$Default', partitionId, { 'startAfterTime' : Date.now()}).then(function(receiver) {
                console.log('Created partition receiver: ' + partitionId)
                receiver.on('errorReceived', printError);
                receiver.on('message', printMessage);
            });
        });
    })
    .catch(printError);


http.listen(3000, function(){
  console.log('listening on *:3000');
});

最佳答案

根据你的代码。每当发送方停止发送时,接收方将不会收到消息,而是等待发送方发送新消息。但是,如果您仍想检查,可以使用发件人消息的序列号或将 ID 与其关联以检查重复项。

关于node.js - 如何检查 Azure IOT 中心发送器是否已停止使用 node.js 和 socket.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45324580/

相关文章:

node.js - 使用OpenCV从X射线图像进行3D重建所需的信息

azure - 流分析条件检查

javascript - 从客户端向服务器发送伪造的 socket.io 请求(node.js/socket.io)

Laravel-echo-server , net::ERR_CONNECTION_TIMED_OUT

node.js - Socket.io 作为客户端进行身份验证

Azure IoT 中心简单的发布-订阅通信

azure - Azure IOT 中成功部署的定义是什么

node.js - 如何让 Typescript 转译我的 Sequelize 模型?

node.js - 将 IBM Conversation 连接到 Watson Workspace?

javascript - res.write 无法正常工作。它显示包含 HTML 标签的输出