2017-04-23 15 views
0

私は時間のためにこれを理解しようとしているが...仕事をしたくはありませんが、それはちょうどdoesnの仕事はありません。エラーメッセージなし、何もありません。nodejsサーバサイドイベントは、ちょうど私が、それを説明し、さまざまなチュートリアルを使用私のコードにコードをコピーし</p> <p>

クライアント:

<script> 
startStream() 
function startStream() 
{ 
    source = new EventSource("/stream") 
    if(typeof(EventSource) !== "undefined") { 
     console.log("streams are supported") 

     source.addEventListener('message', function(e) { 
      console.log(e.data); 
     }, false); 

     source.onerror = function(event) { 
      source.close(); 
     } 
    } else { 
     console.log("no stream support") 
    } 
} 
</script> 

がサーバ:ここ

は私がやっているものですここ

http = require('http') 
fs = require('fs') 

var server = http.createServer(function(request, response) 
{ 
    //understand the request 
    var path = request.url 
    if(path == "/") 
     path = "/index.html" 
    console.log("request for "+path) 
    if (path.indexOf("stream") != -1) 
    { 
     console.log("- client requesting stream") 
     response.writeHead(200, {"Content-Type":"text/event-stream", "Cache-Control":"no-cache", "Connection":"keep-alive"}) 
     response.write('\n') 

     var interval = setInterval(function() { 
      console.log("- sending stream data") 
      response.write("data: message") 
      response.write('\n') 
     }, 1000); 

     request.connection.addListener("close", function() { 
       clearInterval(interval); 
     }, false); 
     return 0 
    } 

    var html = fs.readFileSync("index.html","utf-8").toString(); 
    response.writeHead(200, {"Content-Type": "text/html"}) 
    response.write(html) 
    response.end() 
}) 

//wait for requests 
var port = 5001 
server.listen(port) 
console.log("listening on port "+port+"...") 

を誰かがまた、ストリームに問題があったStackOverflowの上で、それは、応答することが示唆されました.end()を挿入する必要があります。その人のために働いた。私はちょうど私がこれを行うときに接続が閉じられた後に書くことができないというエラーを得る。これは、( "エラー:終了後に書き込み")魔法のようなものだ

サーバー出力:

request for /stream 
- client requesting stream 
- sending stream data 
- sending stream data 
- sending stream data 
- sending stream data 
- sending stream data 

クライアント出力:

streams are supported 

クライアントレスポンスヘッダ:

HTTP/1.1 200 OK 
Content-Type: text/event-stream 
Cache-Control: no-cache 
Connection: keep-alive 
Date: Sun, 23 Apr 2017 09:48:04 GMT 
Transfer-Encoding: chunked 

クライアントのリクエストヘッダ:

GET /stream HTTP/1.1 
Host: localhost:5000 
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0 
Accept: text/event-stream 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate 
Referer: http://localhost:5000/ 
Cookie: 0f62446157da624a2edb8a2b53d86dc1=de-DE 
Connection: keep-alive 
Pragma: no-cache 
Cache-Control: no-cache 

は何もありませんもっとまもなく質問を投稿した後、最終的にはこの

答えて

0

良い神以外のネットワークインスペクタで起こっ

各メッセージは、エンド

response.write("data: message") 
response.write('\n\n') 
のn \ TWOが含まれている必要が
関連する問題

 関連する問題