我们正在开发一个显示邀请数的应用程序。该应用程序允许用户发送批准其注册资料的请求。同一配置文件将在数据库中更新。请求的邀请应作为通知显示给管理员。管理员可以接受或拒绝请求。对于此功能,我尝试了HTML5服务器发送事件。但是,浏览器一直在旋转。
我的问题是:
- 获得数据库结果的正确方法是什么?
- 如何在通知面板中显示邀请计数?
index.JSP
<div id="result"></div>
<script>
if(typeof(Eventsource) !== "undefined") {
var source = new Eventsource("test");
source.onmessage = function(event) {
document.getElementById("result").innerHTML += event.data + "<br>";
};
} else {
document.getElementById("result").innerHTML = "Sorry,your browser does not support server-sent events...";
}
</script>
控制器
@Controller
public class HelloWorldController {
@GetMapping(path = "/test",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> streamFlux() {
return Flux.interval(Duration.ofSeconds(1)).map(sequence -> "Flux - " + LocalTime.now().toString());
}
}