与服务器断开连接并重新连接后,功能会使运行中的实例加倍

时间:2019-02-14 14:26:20

标签: javascript node.js events

我有两个在本地环境中运行的单独的节点服务器,我正在使用其中一个侦听另一个事件。当事件服务器断开连接并重新连接时,侦听服务器的功能将加倍,从而导致其发送回多个dbConfirm事件,并将其多次插入我的mongoDB中。在尝试重新连接之前,我该如何进行冷却,并确保仅运行其中一个实例?

function allData() {
  var url = 'http://10.10.10.1:8080/v1/Events/?access_token=' + token;
  var esInitDict = {
    rejectUnauthorized: false
  };
  var es = new EventSource(url, esInitDict)
  // Only fires for Spark URL
  es.addEventListener('open', function (e) {
    console.log('Event Stream to Spark-server Connected!')
  }, false);
  // fires for allData event
  es.addEventListener('allData', function (e) {  
  //if the data recieved from the photon is null send back an even for file deletion purposes but dont push to DB
    if(e.data==null){
      var publishEventPr = particle.publishEvent({
        name: 'dbConfirm',
        data: 'Null',
        auth: token
      });
      publishEventPr.then(
        function (data) {
          if (data.body.ok) {
            console.log("Document Was recieved, but data is Null. Was not inserted into DB.")
          }
        },
        function (err) {
          console.log("Failed to publish event: " + err)
        }
      );
    }else{ 
    var parsedData = JSON.parse(e.data);
    let ProbeDataObject = {
      [parsedData.coreid]: parsedData.data,
      "published": parsedData.published_at
    }
    let dataString = ProbeDataObject[parsedData.coreid].split(',')
    //send data object to db dunction
    sendToDB(ProbeDataObject)
    //check if any alarms have been triggered
    let currentTime = new Date()
    if (Object.keys(alarmData).length>1)
    {alarmData.forEach(element => {
      if (parsedData.coreid == element.id) {
        if (element.data.alertTime < currentTime.toISOString()) {
          axios.post("http://10.10.10.1:3000/sendnotification", {
            to: element.data.email,
            data: ('User alarm for ' + element.id+ ' set for ' + element.data.alertTime)
          })
        } else if (parseFloat(element.data.rbAlertOver) <= parseFloat(dataString[3])) {
          axios.post("http://10.10.10.1:3000/sendnotificion", {
            to: element.data.email,
            data: (element.id + "'s Sugar Coversion has exceeded user defined value of " + element.data.rbAlertOver + ' at ' + currentTime + ' with a readeing of ' + dataString[5])
          })
        } 
        else if (parseFloat(element.data.rbAlertUnder) >= parseFloat(dataString[3])) {
          axios.post("http://10.10.10.1:3000/sendnotificion", {
            to: element.data.email,
            data: (element.id + "'s Sugar Coversion is under user defined value of " + element.data.rbAlertUnder + ' at ' + currentTime + ' with a readeing of ' + dataString[5])
          })}
          else if (parseFloat(element.data.srmAlertOver) <= parseFloat(dataString[22])) {
          axios.post("http://10.10.10.1:3000/sendnotification", {
            to: element.data.email,
            data: (element.id + "'s SRM has exceeded user defined value of " + element.data.srmAlertOver + ' at ' + currentTime + ' with a readeing of ' + dataString[25])
          })
        } 
        else if (parseFloat(element.data.srmAlertUnder) >= parseFloat(dataString[22])) {
          axios.post("http://10.10.10.1:3000/sendnotification", {
            to: element.data.email,
            data: (element.id + "'s SRM is under user defined value of " + element.data.srmAlertUnder + ' at ' + currentTime + ' with a readeing of ' + dataString[25])
          })
        } 
        else if (parseFloat(element.data.tempAlertOver) <= parseFloat(dataString[13])) {
          axios.post("http://10.10.10.1:3000/sendnotification", {
            to: element.data.email,
            data: (element.id + "'s temperature has exceeded user defined value of " + element.data.tempAlertOver + ' at ' + currentTime + ' with a readeing of ' + dataString[15])
          })
        }
        else if (parseFloat(element.data.tempAlertUnder) >= parseFloat(dataString[13])) {
          axios.post("http://10.10.10.1:3000/sendnotification", {
            to: element.data.email,
            data: (element.id + "'s temperature is under user defined value of " + element.data.tempAlertUnder + ' at ' + currentTime + ' with a readeing of ' + dataString[15])
          })
        }
         else if (parseFloat(element.data.turbidAlertOver) <= parseFloat(dataString[25])) {
          axios.post("http://10.10.10.1:3000/sendnotification", {
            to: element.data.email,
            data: (element.id + "'s turbidity has exceeded user defined value of " + element.data.turbidAlertOver + ' at ' + currentTime + ' with a readeing of ' + cat[28])
          })
        }
        else if (parseFloat(element.data.turbidAlertUnder) >= parseFloat(dataString[25])) {
          axios.post("http://10.10.10.1:3000/sendnotification", {
            to: element.data.email,
            data: (element.id + "'s turbidity is under user defined value of " + element.data.turbidAlertUnder + ' at ' + currentTime + ' with a readeing of ' + cat[28])
          })
        }else if (true){
          console.log('no alarms triggered')
        }
      }
    })}}
  }, false);

  es.addEventListener('error', function (e) {
    console.log("Error, check that spark-server is running");

  }, false);
}```

0 个答案:

没有答案