Java HTML5服务器端事件(SSE)
我正在使用servlet在Java堆栈上的Web应用程序中实现SSE。我目前面临两个关键问题。首先让我将网页和Servlet的代码放在前面,然后是我面临的问题 网页代码: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
<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代码工作得很好,尽管我在这方面做得不多(曾经做过一个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
。也许其中一个浏览器不支持它