2016-04-20 4 views
1

クライアント側では、次のようになります。なぜこのイベントソースはメッセージとエラーを繰り返しトリガしていますか?

var es = new EventSource("http://localhost:8080"); 
es.addEventListener("message", function(e) { 
    alert("e.data") //Alerts "" every couple seconds 
}) 
es.addEventListener("error", function(e) { 
    alert("error") //Also fires every couple of seconds 
}) 
var post_request = new XMLHttpRequest(); 
post_request.open("POST", "http://localhost:8080"); 
post_request.setRequestHeader("Content-Type", "text/plain"); 
post_request.addEventListener("readystatechange", function() { 
    if (post_request.readyState == 4 && post_request.status == 200) { 
     alert(post_request.responseText); //This works 
    } 
}) 
post_request.send("This is a test") 

サーバー側はPOSTリクエストを処理するのNode.jsのようになります。私は、クライアント側からPOSTリクエストデータを送信する場合、それは私に返される

function process_request(request, response) { 
    var request_body = [] 
    request.on("data", function(chunk) { 
     request_body.push(chunk) 
    }) 
    request.on("end", function() { 
     request_body = Buffer.concat(request_body).toString()+"\n" 
     response.writeHead(200, {"Access-Control-Allow-Origin": "*", 
           "Content-Type": "text/event-stream", 
           "Connection": "keep-alive" 
           }); 

     response.end("data: " + request_body + "\n"); 
    }) 
} 

response.end()と予想されますが、esは、数秒ごとにmessageイベントに加えて、2秒ごとにエラーをトリガーしています。しかし、messageイベントがトリガされたときには""が警告されます。理由はわかりません。誰も私がこの行動を把握するのを助けることができますか?

編集:messageerrorイベントでes.readyStateをチェックしただけです。 readyStateerror0なので、切断された可能性があります。この繰り返しの切断はなぜ起こるのですか?なぜ繰り返し接続と切断が繰り返されるのでしょうか?messageイベントが繰り返されますか?

+0

「これはテストです」は 'text/event-stream'コンテンツでは有効ではありません。あなたは実際に有効な応答の本文を書こうとしましたか? – mscdex

+0

@mscdex私はそれを有効な 'text/event-stream'コンテンツにするために編集しました。ここで何が起きるかは、 'es'が' message'イベントと 'error'イベントの両方を数秒ごとに引き起こすということです。しかし、 'message'イベントの' alert(e.data) 'は空文字列を警告しますか? – digglemister

答えて

1

何が起こっているのかは、ユーザーのSSE要求を処理して、データを送り返してから接続を閉じることです。クライアントは接続が失われていると判断し、数秒後に再接続します(この再接続はSSEの機能です)。

だから、決して終了しないでください。つまり、request.on("end", ...に決して届かないようにする必要があります。ここで

は、私が前に使ってきたのNode.jsの基本的SSEサーバーです:

var http = require("http"); 
var port = parseInt(process.argv[2] || 8080); 

http.createServer(function(request,response){ 
    console.log("Client connected:" + request.url); 
    response.writeHead(200, { "Content-Type": "text/event-stream" }); 
    var timer = setInterval(function(){ 
    var content = "data:" + new Date().toISOString() + "\n\n"; 
    response.write(content); 
    }, 1000); 
    request.connection.on("close", function(){ 
    response.end(); 
    clearInterval(timer); 
    console.log("Client closed connection. Aborting."); 
    }); 
    }).listen(port); 
console.log("Server running at http://localhost:" + port); 

すなわち、私たちはsetIntervalを使い続けています。リッスンする唯一のイベントは、クライアントが接続を閉じることです。

+0

ありがとうございます。あなたは '' Connection ':応答ヘッダー内の "keep-alive" 'が何をするのかを説明できますか? – digglemister

+1

キープアライブのStackOverflowタグ全体があります:http://stackoverflow.com/questions/tagged/keep-alive SSEで使う必要はないと思います。 –

関連する問題