2016-05-17 6 views
0

サーバー側イベントでは、イベントソースはHTTP接続を介してサーバーに接続し、MDNドキュメントごとに接続を閉じることなく、イベントをテキスト/イベントストリーム形式で受信します。ブラウザが開い サーバー側イベント接続の問題

/** 
 
* 
 
*/ 
 

 
(function(){ 
 
\t "use strict" 
 
\t var init = function(){ 
 
\t \t if (window.EventSource) { 
 
\t \t \t console.log("Event source available"); 
 
\t \t \t var source = new EventSource('/message'); 
 

 
\t \t \t source.addEventListener('message', function(e) { 
 
\t \t \t  var container = document.getElementById("container"); 
 
\t \t \t  container.innerHTML += e.data; 
 
\t \t \t }); 
 

 
\t \t \t source.addEventListener('open', function(e) { 
 
\t \t \t   console.log("Connection was opened."); 
 
\t \t \t }, false); 
 

 
\t \t \t source.addEventListener('error', function(e) { 
 
\t \t \t   if (e.readyState == EventSource.CLOSED) { 
 
\t \t \t    console.log("Connection was closed."); 
 
\t \t \t   } else { 
 
\t \t \t    console.log(e.readyState);  
 
\t \t \t   } 
 
\t \t \t }, false); 
 
\t \t \t } else { 
 
\t \t \t   console.log("No SSE available"); 
 
\t \t \t } 
 
\t } 
 
\t init(); 
 
\t 
 
})();
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<title>Index file</title> 
 
<script type="text/javascript" src="js/ssescript.js"></script> 
 
</head> 
 
<body> 
 
\t This is the index file. 
 
\t <div id="container"></div> 
 
</body> 
 
</html>
は、サーバー側で私はSpring MVCの

package com.example.controller; 

import java.util.Random; 

import javax.servlet.http.HttpServletResponse; 

import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 


@RestController 
public class AppController { 


    @RequestMapping("/message") 
    public String getMessage(HttpServletResponse response){ 
     response.setContentType("text/event-stream"); 
     Random r = new Random(); 
     return "data:Testing 1,2,3 "+r.nextInt()+"\n\n"; 
    } 

} 

たびを使用しています -

https://developer.mozilla.org/en-US/docs/Web/API/EventSource

私は、次のコードを持っています新しいコネクターそれを閉じます。 EventSource接続を一度だけ開くことができ、サーバーからデータをプッシュできますか?

+0

は、ブラウザからのサポートを必要とするようです;) – Blank

+0

は、私はGoogleのChromeでそれを試してみました50.0.2661.102 –

+0

何に '@ResponseBodyを'追加についてあなたのコントローラメソッド? – Blank

答えて

0

この同じエラーが発生しました。

多くの試行錯誤の末、Springのデフォルト設定のためにTomcatが接続を閉じることが判明しました。非同期要求のタイムアウトを、私のニーズに対応できる大きさに設定しました。

spring.mvc.async.requestタイムアウト=(いくつかの非常に大きな数)

関連する問題