Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
服务器在Java Web服务器上发送了事件_Java_Spring_Rest_Notifications_Server Sent Events - Fatal编程技术网

服务器在Java Web服务器上发送了事件

服务器在Java Web服务器上发送了事件,java,spring,rest,notifications,server-sent-events,Java,Spring,Rest,Notifications,Server Sent Events,我有一个使用Spring框架的JavaWeb服务器,我想使用服务器发送的事件每秒向Web客户端发送通知 这些通知的我的控制器如下所示: @Controller public class NotificationController { private static final String REST_PREFIX = "/rest/notifications"; @RequestMapping(value = {REST_PREFIX}, method = {RequestM

我有一个使用Spring框架的JavaWeb服务器,我想使用服务器发送的事件每秒向Web客户端发送通知

这些通知的我的控制器如下所示:

@Controller
public class NotificationController {

    private static final String REST_PREFIX = "/rest/notifications";

    @RequestMapping(value = {REST_PREFIX}, method = {RequestMethod.GET})
    synchronized public void getMonitoringNotifications(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/event-stream;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Connection", "keep-alive");

        try {
            PrintWriter out = response.getWriter();
            int i = 0;

            while (true) {
                out.print("id: " + "ServerTime" + "\n");
                out.print("data: " + (i++) + "\n\n");
                out.flush();

                Thread.currentThread().sleep(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
我的问题是,客户端不是在1秒后接收每个通知,而是等待发送所有通知。 如果我尝试在客户端上发送一些通知,比如说30个,我最终会收到所有通知

我的客户端很简单,因为它只侦听特定端点以获取通知:

<!DOCTYPE html>
<html>
<body>
 <h1>Notification received  : </h1>

 <div id="ServerTime"></div>

 <script>
  if (typeof (EventSource) !== "undefined") {
   var source = new EventSource("https://10.241.53.185/rest/notifications");
   source.addEventListener('message', function(event) {
    console.log(event.data);
   });
  } else {
   document.getElementById("ServerTime").innerHTML = "Working, processing, getting info....";
  }
 </script>

</body>
</html>

收到的通知:
if(typeof(EventSource)!=“未定义”){
变量源=新事件源(“https://10.241.53.185/rest/notifications");
source.addEventListener('message',函数(事件){
console.log(事件数据);
});
}否则{
document.getElementById(“ServerTime”).innerHTML=“工作、处理、获取信息…”;
}
你能帮我解决这个问题吗


谢谢

您需要使用SSEEmiter发送事件。见(博客)


您还应该从单独的线程发送事件(让请求线程返回)。

您需要使用SSEEmiter发送事件。见(博客)


您还应该从单独的线程发送事件(让请求线程返回)。

这显然不是向客户端发送信息的方式。使用
SSEEmitor
StreamingResponseBody
作为结果。感谢您提供的信息。我还发现我的代码有问题,因为我需要以不同的线程写入打印机。这显然不是向客户发送信息的方式。使用
SSEEmitor
StreamingResponseBody
作为结果。感谢您提供的信息。我还发现我的代码有问题,因为我需要用不同的线程写入打印机。谢谢,这真的很有帮助。谢谢,这真的很有帮助。