检查下面的代码,这里我已经为每个事件类型添加了三条警报消息,但在此代码中source.onopen()[alert:readyState:1]和source.onerror()[alert:readyState:0]正常工作但在onmessage()的情况它没有被执行
if(typeof(EventSource) !== "undefined") {
var source = new EventSource('clinic/get');
source.onopen = function(){
alert('connection is opened.'+source.readyState);
};
source.onerror = function(){
alert('error: '+source.readyState);
};
source.onmessage = function(datalist){
alert("message: "+datalist.data);
};
} else {
document.getElementById("clinic-dtls").innerHTML = "Sorry, your browser does not support server-sent events...";
}`
Run Code Online (Sandbox Code Playgroud)
检查服务器端的以下代码
Random random = new Random();
response.setContentType("text/event-stream");
response.setCharacterEncoding("UTF-8");
System.out.println("clinic/get got hit");
try {
Writer out = response.getWriter();
out.write("data: welcome data"+random);
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我正在使用STS(Spring工具套装),我想知道,当我在EventSource(源)对象上使用ctrl + space时,在选项中它只显示onopen()和onerror(),其中是onmessage(). …