2016-10-19 36 views
0

我試圖設置節點js服務器通過SSE流事件,我不能讓它工作,除非我開始與data字段的流。服務器發送事件的數據未被髮送以外的字段?

當我嘗試幷包括其他字段,例如idevent他們不顯示在鉻檢查器?

如果我試圖把這些字段放在數據之前,除了連接打開之外沒有其他事件發生。

這是我玩的路線。

router.get('/stream', function *(){ 
    let stream = new PassThrough(); 
    let send = (message, id) => stream.write(`id: ${JSON.stringify(id)}\n data: ${JSON.stringify(message)}\n\n`);  
    let finish =() => dispatcher.removeListener('message', send);  
    this.socket.setTimeout(Number.MAX_VALUE); 
    this.type = 'text/event-stream;charset=utf-8'; 
    this.set('Cache-Control', 'no-cache'); 
    this.set('Connection', 'keep-alive');  
    this.body = stream; 
    stream.write(': open stream\n\n'); 

    dispatcher.on('message', send); 
    this.req.on('close', finish); 
    this.req.on('finish', finish); 
    this.req.on('error', finish); 
    setInterval(function(){ 
     dispatcher.emit('message', {date: Date.now()}, '12345') 
    }, 5000) 
}); 

我不太確定如果我正確寫入流。

謝謝

回答

0

傻,我的確是一個畸形的事件流。不應該有田地之間的空間。它應該是:

id: ${JSON.stringify(id)}\ndata: ${JSON.stringify(message)}\n\n