【问题标题】:HTML5 Server Side Events (SSE)HTML5 服务器端事件 (SSE)
【发布时间】:2014-09-04 10:59:28
【问题描述】:

我正在使用 Servlet 在 Java Stack 上的 Web 应用程序中实现 SSE。我目前面临两个关键问题。首先让我为网页和 Servlet 编写代码,然后是我面临的问题。

网页代码:

<script type="text/javascript">
function registerSSE() {
var source = new EventSource("http://www.sample.com/BootStrap/NotificationServlet");
source.addEventListener('StartProductionRun', function(e) {
    // Get the data and identify the instrument Name/Id
    var dataReceived = e.data;
    document.getElementById(dataReceived + "_button").disabled = true;
    }, false);
}

function StartProduction(instrument) {
var dataString = 'instrumentName='+ instrument; 

// call ajax to submit the form and start the production  run
$.ajax({
    type: 'POST',
    url: 'http://localhost:8080/BootStrap/ProductionRunServlet',
    data: dataString,
    success: function() {
        $('#Status').html("<div id='message'></div>"); 
        $('#message').html("<h4 aling=\"centre\">Prudction Run for Instrument " + instrument  + " initiated.</h4>")
        .hide()
        .fadeIn(5000);
    }
});
}

</script>

Servlet 代码:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/event-stream;charset=UTF-8");
    response.setHeader("Cache-Control", "no-cache");

    PrintWriter out = response.getWriter();
    NotificationService notificationService = null;

    while (true) {
        notificationService = NotificationService.getInstance();

        if (notificationService.getNotificationCount() > 0 ) {

            String notificationValue = notificationService.getNotification(0);
            String[] keyValue = notificationValue.split(":");

            out.print("event:" + keyValue[0] + "\n");
            out.print("data: " + keyValue[1] + "\n");
            out.print("retry:" + 1000 + "\n\n");

            out.flush();
        }
        else {
            out.print(": time stream \n");
            out.print("retry:" + 1000 + "\n\n");
            out.flush();
        }

       try {
             Thread.sleep(1000);
            } catch (InterruptedException e) {
             e.printStackTrace();
            }
    }
}

现在的问题:

  1. 该网页将被多个用户同时查看。我希望将数据推送给查看该页面的所有用户。目前,当我在我的机器上本地运行时,即使我打开 Chrome 和 Firefox,我也没有在两个浏览器中收到通知。它只有一个。
  2. 另外,如果我让浏览器运行一段时间,我发现即使 servlet 基于某些事件推送数据。我没有在浏览器上收到通知。

我需要确保: 通知会推送给正在查看该特定页面的所有客户,无论他们在该页面上做什么,或者该页面仅用于查看信息。

期待我能获得的所有帮助以使其正常运行。另外,我很想知道是否有其他替代方法可以使用。

【问题讨论】:

    标签: java html servlets server-sent-events


    【解决方案1】:

    您的 servlet 代码运行良好,虽然我没有做太多工作(曾经做过一个 jsp 项目)。我认为,你错过了javascript中的一些东西? 我认为javascript中也应该有一个计时器/线程/循环,以连续获取所有推送的数据。即,

    setInterval(
    function(){
    // code which needs to run every 5sec
    },5000);
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      您应该在使用之前检查EventSource 在该浏览器中是否可用。也许其中一个浏览器不支持它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-19
        • 1970-01-01
        • 2012-06-01
        • 2021-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多