如何增加服务器发送事件重新打开时间?
How to increase Server-Sent Event's reopen time?
我正在努力显示来自服务器发送事件的通知。我检查了每次浏览器在每次连接关闭后大约 3 秒尝试重新连接源。该事件的调用速度太快,因此我的服务器也已加载。
那么如何更改重新开放时间以增加?我必须至少做 60 秒,所以请告诉我怎么做?
我正在尝试以下代码。
<table class="table" id="notification"></table>
<script type="text/javascript">
var ssevent = null;
if (!!window.EventSource) {
ssevent = new EventSource('ssevent.php');
ssevent.addEventListener('message', function(e){
if(e.data){
json = JSON.parse(e.data);
if(json){
json.forEach(function(v,i){
html = "<tr><td>"+ v.text +"</td><td>"+ v.date +"</td></tr>";
});
$('#notification').append(html);
}
}
}, false);
ssevent.addEventListener('error', function(e) {
if (e.readyState == EventSource.CLOSED){
console.log("Connection was closed.");
}
}, false);
} else {
console.log('Server-Sent Events not support in your browser');
}
</script>
事件流文件如下
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
include_once "config.php";
$time = isset($_SESSION['last_event_time']) ? $_SESSION['last_event_time'] : time();
$result = $db->quesry("SELECT * FROM event_noti WHERE event_time < {$time} ")->rows;
$_SESSION['last_event_time'] = time();
if($result){
$json = array();
foreach ($result as $row){
$json[] = array(
'text' => $row['event_text'],
'date' => date("Y-m-d H:i", $row['event_time']),
);
}
echo "data: ". json_encode($json) . "\n\n";
flush();
}
从根本上说,您无法控制它:它是特定于浏览器的设置。
如果您的浏览器是 Firefox,它似乎受此设置控制:dom.server-events.default-reconnection-time
默认值为 5000 毫秒。
退后一步:只有在 服务器 关闭连接时才会重新连接。你为什么要关闭连接? (*) 为什么 3 秒重连太快了?
SSE 的重点是最小化延迟;权衡是更多的资源使用,特别是必须为每个客户端保持一个专用的套接字打开。
听起来您不想使用 SSE,而是想在 60 秒的 setInterval()
调用中使用简单的 AJAX 投票?
*:如果您确实打算让它保持打开状态,则需要将查询包装起来并在 while(true){...}
循环中处理结果代码。把例如在 while 循环结束时休眠一秒钟以阻止数据库服务器过载。
现在我有了答案。
控制重新连接超时:
浏览器尝试在每个服务器发送的事件连接关闭后约 3 秒内重新连接到源。在尝试重新连接之前,您可以通过以 retry:
开头的行然后添加要等待的毫秒数来更改超时。
我更改了下面的代码并按照我的意愿开始工作。
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
include_once "config.php";
$time = isset($_SESSION['last_event_time']) ? $_SESSION['last_event_time'] : time();
$result = $db->quesry("SELECT * FROM event_noti WHERE event_time < {$time} ")->rows;
$_SESSION['last_event_time'] = time();
echo "retry: 60000\n"; // 60 seconds, to wait for next connection.
$json = array();
if($result){
foreach ($result as $row){
$json[] = array(
'text' => $row['event_text'],
'date' => date("Y-m-d H:i", $row['event_time']),
);
}
}
echo "data: ". json_encode($json) . "\n\n";
flush();
我正在努力显示来自服务器发送事件的通知。我检查了每次浏览器在每次连接关闭后大约 3 秒尝试重新连接源。该事件的调用速度太快,因此我的服务器也已加载。
那么如何更改重新开放时间以增加?我必须至少做 60 秒,所以请告诉我怎么做?
我正在尝试以下代码。
<table class="table" id="notification"></table>
<script type="text/javascript">
var ssevent = null;
if (!!window.EventSource) {
ssevent = new EventSource('ssevent.php');
ssevent.addEventListener('message', function(e){
if(e.data){
json = JSON.parse(e.data);
if(json){
json.forEach(function(v,i){
html = "<tr><td>"+ v.text +"</td><td>"+ v.date +"</td></tr>";
});
$('#notification').append(html);
}
}
}, false);
ssevent.addEventListener('error', function(e) {
if (e.readyState == EventSource.CLOSED){
console.log("Connection was closed.");
}
}, false);
} else {
console.log('Server-Sent Events not support in your browser');
}
</script>
事件流文件如下
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
include_once "config.php";
$time = isset($_SESSION['last_event_time']) ? $_SESSION['last_event_time'] : time();
$result = $db->quesry("SELECT * FROM event_noti WHERE event_time < {$time} ")->rows;
$_SESSION['last_event_time'] = time();
if($result){
$json = array();
foreach ($result as $row){
$json[] = array(
'text' => $row['event_text'],
'date' => date("Y-m-d H:i", $row['event_time']),
);
}
echo "data: ". json_encode($json) . "\n\n";
flush();
}
从根本上说,您无法控制它:它是特定于浏览器的设置。
如果您的浏览器是 Firefox,它似乎受此设置控制:dom.server-events.default-reconnection-time
默认值为 5000 毫秒。
退后一步:只有在 服务器 关闭连接时才会重新连接。你为什么要关闭连接? (*) 为什么 3 秒重连太快了?
SSE 的重点是最小化延迟;权衡是更多的资源使用,特别是必须为每个客户端保持一个专用的套接字打开。
听起来您不想使用 SSE,而是想在 60 秒的 setInterval()
调用中使用简单的 AJAX 投票?
*:如果您确实打算让它保持打开状态,则需要将查询包装起来并在 while(true){...}
循环中处理结果代码。把例如在 while 循环结束时休眠一秒钟以阻止数据库服务器过载。
现在我有了答案。
控制重新连接超时:
浏览器尝试在每个服务器发送的事件连接关闭后约 3 秒内重新连接到源。在尝试重新连接之前,您可以通过以 retry:
开头的行然后添加要等待的毫秒数来更改超时。
我更改了下面的代码并按照我的意愿开始工作。
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
include_once "config.php";
$time = isset($_SESSION['last_event_time']) ? $_SESSION['last_event_time'] : time();
$result = $db->quesry("SELECT * FROM event_noti WHERE event_time < {$time} ")->rows;
$_SESSION['last_event_time'] = time();
echo "retry: 60000\n"; // 60 seconds, to wait for next connection.
$json = array();
if($result){
foreach ($result as $row){
$json[] = array(
'text' => $row['event_text'],
'date' => date("Y-m-d H:i", $row['event_time']),
);
}
}
echo "data: ". json_encode($json) . "\n\n";
flush();