2016-08-30 72 views
1

我有事件監聽器,它由SSE(服務器發送事件)觸發,我如何將數據發佈到其他服務器。SSE(服務器發送事件),監聽Node.js服務器中的事件並將其發佈到其他服務器

NODE JS

//including Event Source 
    var EventSource = require('eventsource');  

    var es = new EventSource('http://api.xyz.com:8100/update/'); 

    //Listening URL Event Sourse 
    es.addEventListener('message', function (e) { 

     //Extract Json 
     var extractData = JSON.parse(e.data); 

     if(extractData.type == 'CALL') 
     { 
      console.log(extractData); 
      //POST DATA 
     } 
    }); 

我需要把數據上傳到其它服務器另一端是PHP

+1

您是否試過Request Method? – Khalid

回答

1

您可以要求圖書館,有關問題的嘗試:how to make an http post request in node js你可以參考這一點。

//including Event Source 
var EventSource = require('eventsource'); 

var request  = require('request'); /*****ADDED******/ 

var es = new EventSource('http://api.xyz.com:8100/update/'); 

//Listening URL Event Sourse 
es.addEventListener('message', function (e) { 

    //Extract Json 
    var extractData = JSON.parse(e.data); 

    if(extractData.type == 'CALL') 
    { 
     console.log(extractData); 

     //you can call a function 
     postDataToServer(extractData); 

    } 
}); 

//Post Details to other server 
function postDataToServer(SendingDetails) 
{ 
    var ServerURL = 'OTHER_SERVAL_URL'; 

    request({ 
     url : ServerURL, 
     method : "POST", 
     json : true, 
     body : SendingDetails 
    }, function (error, response, body){ 
     console.log(body);  
    }); 
} 
相關問題