Agu*_*pez 12 javascript java html5 spring-mvc
我目前遇到与SSE和Windows XP相关的问题.以下源代码目前适用于我尝试过的每个Chrome,但Windows XP中的Chrome除外(?)不确定原因.这旨在用于控制面板,用户必须使用Chrome.换句话说,我不关心IE,Firefox等.
问题:服务器端事件无处不在(Chrome),但不适用于Windows XP(Chrome).当我说它有效时,我的意思是调用消息处理程序.
代码
Javascript代码
if (!!window.EventSource) {
console.log("Event source available");
var source = new EventSource('/admin/systemalert');
source.addEventListener('message', function(e) {
console.log(e.data);
});
source.addEventListener('open', function(e) {
console.log("Connection was opened.");
}, false);
source.addEventListener('error', function(e) {
if (e.readyState == EventSource.CLOSED) {
console.log("Connection was closed.");
} else {
console.log(e.readyState); <-- in windows XP it prints Error here
}
}, false);
} else {
console.log("No SSE available");
}
Run Code Online (Sandbox Code Playgroud)服务器端代码
@Controller
@RequestMapping("/admin/**")
public class AdminController {
@RequestMapping("systemalert")
public @ResponseBody String sendMessage(HttpServletResponse response) {
Random r = new Random();
response.setContentType("text/event-stream");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "data:Testing 1,2,3" + r.nextInt() +"\n";
}
}
Run Code Online (Sandbox Code Playgroud)如代码中所述,行console.log(e.readyState); 在Windows XP中使用Chrome时打印"错误".有任何想法吗?有人看到源代码有什么问题吗?
提前致谢.奥古斯丁
Agu*_*pez 11
对于有此问题的任何人,问题与数据之后所需的新行有关.基本上,你需要两条线而不是我使用的一条线.这样它无处不在.
改变这个:
return "data:Testing 1,2,3" + r.nextInt() +"\n";
Run Code Online (Sandbox Code Playgroud)
对此:
return "data:Testing 1,2,3" + r.nextInt() +"\n\n";
Run Code Online (Sandbox Code Playgroud)
修复问题..
| 归档时间: |
|
| 查看次数: |
14456 次 |
| 最近记录: |