简体   繁体   English

如何使用node.js和socket.io检查Azure IOT Hub发送方是否已停止

[英]How to check if Azure IOT Hub sender has stopped using node.js and socket.io

I have two programs, a sender and a receiver. 我有两个程序,一个发送器和一个接收器。 The sender sends some message to device on IOT Hub and receiver in turn receives those messages as long as sender sends them. 发送者将一些消息发送到IOT集线器上的设备,而接收者又会依次接收这些消息,只要发送者发送它们即可。 I am using socket.io to broadcast those messages to connected clients. 我正在使用socket.io将这些消息广播到已连接的客户端。 However when the sender is stopped, the receiver also stops but the last message sent by sender is going to broadcasted infinitely until i close the receiver or the sender starts again and sends new messages. 但是,当发送方停止时,接收方也将停止,但发送方发送的最后一条消息将无限广播,直到我关闭接收方或发送方再次启动并发送新消息为止。 The last message will get duplicated and broadcasted infinitely. 最后一条消息将被复制并无限广播。 How to check if the sender program has stopped? 如何检查发送程序是否已停止?

this is the sender prrogram: 这是发件人的图表:

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);

this is the receiver and the socket.io which broadcasts to the client: 这是向客户端广播的接收方和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');
});

According to your code. 根据您的代码。 Whenever the sender stops sending, the receiver will not receive the messages and will wait for the sender to send new messages. 每当发件人停止发送时,收件人将不会接收到消息,而是将等待发件人发送新消息。 However, if you still want to check, you can use sequence numbers with sender messages or associate Id with them to check duplicates. 但是,如果仍然要检查,则可以将序列号与发件人消息一起使用,或者将ID与它们关联,以检查重复项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM