[英]Avoid same data with Server-Sent Events
I'm using the Server-Sent Events to push new data to users through a Webapp. 我正在使用服务器发送事件通过Webapp向用户推送新数据。 I'd like to avoid to display twice the same data.
我想避免显示两次相同的数据。 My idea was to use the e.lastEventId as Mozilla's documentation recommends.
我的想法是按照Mozilla的文档建议使用e.lastEventId。
Data displayed in test_stream.php : 显示在test_stream.php中的数据:
id: 3 data: test id:3数据:测试
JS Script : JS脚本:
var source = new EventSource('test_stream.php');
source.onmessage = function(e) {
var now_id = e.lastEventId;
if(last_id != now_id) {
var last_id = e.lastEventId;
document.body.innerHTML += e.data + '<br>';
}
};
Problem : 问题:
"test" is displayed every 3 sec instead of only one until new data come. 每3秒钟显示一次“测试”,而不是每秒钟显示一次,直到出现新数据。 I get well the ID but it looks like if my 'if' in the js script doesn't work... Any idea ?
我得到了很好的ID,但看起来如果我的js脚本中的“ if”不起作用...知道吗?
var source = new EventSource('test_stream.php');
var last_id;
source.onmessage = function(e) {
var now_id = e.lastEventId;
if(last_id != now_id) {
last_id = e.lastEventId;
document.body.innerHTML += e.data + '<br>';
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.