当服务端的会话计时器下降到5分钟以下时,我需要向用户发送一个事件。我正在尝试使用HTML5的EventSource,因为我只想向服务器发送一个单一请求(多个请求将每次重置会话计时器)。根据Lean Java Engineering的说法,HTML5满足我的需求。
-Javascript-
console.log("Starting eventSource");
var eventSource = new EventSource("SSETest/push");
console.log("Started eventSource");
eventSource.onmessage = function(event) { console.log("Message received: " + event.data); };
eventSource.onopen = function(event) { console.log("Open " + event); };
eventSource.onerror = function(event) { console.log("Error " + event); };
console.log("eventState: " + eventSource.readyState);
// Stop trying after 10 seconds of errors
setTimeout(function() {eventSource.close();}, 10000);
-Grails控制器-
package sessionManager
class SSETestController {
def push = {
println "$request made it!"
response.setContentType("text/event-stream, charset=UTF-8")
response << "data: the data"
render "HI"
}
}
-Grails 控制台-
org.apache.catalina.core.ApplicationHttpRequest@4f889fad made it!
org.apache.catalina.core.ApplicationHttpRequest@13f78b8c made it!
org.apache.catalina.core.ApplicationHttpRequest@4b50734c made it!
org.apache.catalina.core.ApplicationHttpRequest@4c4bde24 made it!
-JavaScript控制台-
Starting eventSource
Started eventSource
eventState: 0
Open [object Event]
Error [object Event]
Open [object Event]
Error [object Event]
Open [object Event]
Error [object Event]
Open [object Event]
Error [object Event]
预先感谢, 克里斯·汉考克