Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.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 HTML5服务器端事件(SSE)_Java_Html_Servlets_Server Sent Events - Fatal编程技术网

Java HTML5服务器端事件(SSE)

Java HTML5服务器端事件(SSE),java,html,servlets,server-sent-events,Java,Html,Servlets,Server Sent Events,我正在使用servlet在Java堆栈上的Web应用程序中实现SSE。我目前面临两个关键问题。首先让我将网页和Servlet的代码放在前面,然后是我面临的问题 网页代码: <script type="text/javascript"> function registerSSE() { var source = new EventSource("http://www.sample.com/BootStrap/NotificationServlet"); source.addEventL

我正在使用servlet在Java堆栈上的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>
现在的问题是:

  • 该网页将由多个用户同时查看。我希望数据被推送到所有浏览该页面的用户。目前,当我在我的机器上本地运行时,即使我打开了Chrome和Firefox,我也不会在这两种浏览器中收到通知。它只有一种
  • 另外,如果让浏览器运行一段时间,我会发现即使servlet基于某些事件推出数据。我在浏览器上没有收到通知
  • 我需要确保: 通知会推送到正在查看该特定页面的所有客户端,而不管他们在该页面上做什么,或者该页面仅用于查看信息


    期待着所有的帮助,我可以得到使这项工作。此外,我还想知道是否有其他替代方案可供我使用。

    您的servlet代码工作得很好,尽管我在这方面做得不多(曾经做过一个jsp项目)。我想的是,你在javascript中遗漏了什么? 我认为javascript中也应该有一个计时器/线程/循环,以连续获取所有推送的数据。i、 e

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

    我希望这会有所帮助。

    您的servlet代码工作得很好,尽管我在这方面做得不多(曾经做过一个jsp项目)。我想的是,你在javascript中遗漏了什么? 我认为javascript中也应该有一个计时器/线程/循环,以连续获取所有推送的数据。i、 e

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

    我希望这会有所帮助。

    在使用该浏览器之前,您应该检查
    EventSource
    是否在该浏览器中可用。可能某个浏览器不支持它。

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