代码之家  ›  专栏  ›  技术社区  ›  Neo

如何检查Azure IOT Hub发件人是否已停止使用节点。js和socket.io

  •  1
  • Neo  · 技术社区  · 7 年前

    这是发送方程序:

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

    这是接收器和插座。向客户端广播的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');
    });
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Bolshoi Booze    7 年前