如何解决如何在PHP中模拟SSE断开连接?
我正在尝试编写前端解决方案以重新连接到流,同时通知用户重新连接时间等。
由于我无权访问服务器,因此我正在制作一个小的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 举报,一经查实,本站将立刻删除。