问题描述
我正在尝试编写前端解决方案以重新连接到流,同时通知用户重新连接时间等。
由于我无权访问服务器,因此我正在制作一个小的PHP脚本来模拟我想要实现的目标。
<?PHP
date_default_timezone_set("America/New_York");
header("Cache-Control: no-cache");
header("Content-Type: text/event-stream");
$stream = true;
function connect() {
$start_time = time();
while (true) {
// Every second,send a "ping" event.
echo "event: message\n";
$curDate = date(DATE_ISO8601);
echo 'data: {"time": "' . $curDate . '"}';
echo "\n\n";
echo "event: message\n";
$curDate = date(DATE_ISO8601);
echo 'data: {"time": "' . (time() - $start_time) . '"}';
echo "\n\n";
if ((time() - $start_time) > 5) {
disconnect();
break;
}
ob_end_flush();
flush();
// Break the loop if the client aborted the connection (closed the page)
if ( connection_aborted() ) break;
sleep(1);
}
}
function disconnect() {
$stream = false;
$start_time = time();
while($stream === false) {
if ((time() - $start_time) > 9000) {
$stream = true;
connect();
}
}
}
connect();
?>
问题是,此停止流,但不会在Javascript中引发onerror事件。
模拟SSE服务器断开连接的方法是什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)