- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
TL;DR 更新:无论是从客户端关闭 EventSource 还是完全关闭客户端,php 都会继续在后端执行并且无法报告 connection_aborted()
的正确值.为什么会这样?
我一直在 Google 和 Stack Overflow 上寻找答案,但没有一个建议的修复程序可以解决这个问题:我有一个简单的页面,使用 JavaScript 和 php 测试服务器发送事件的功能。一切都运行良好,直到我意识到当客户端导航到另一个页面或刷新自身时服务器脚本执行并没有停止。这似乎是一个常见问题,其他问题中给出的建议对我没有任何结果。
StackOverflow questions I've already investigated
PHP Event Source keeps executing (the comments on the accepted answer here almost led to something and then they pulled out to "continue this discussion in chat" and that link is apparently broken)
How do I put this on Real-Time? I already put (async: True) but it doesnt work
Linked articles & other material I've already investigated
connection_aborted
继续报道
false
在显式
EventSource.close()
之后在 10 秒的服务器循环完成之前调用客户端或简单地关闭客户端。这是我的确切代码,在删除除服务器发送的事件之外的所有内容后:
document.addEventListener('DOMContentLoaded', () => {
// Set up EventSource for receiving server-sent events.
const testEventSource = new EventSource('sse_tests.php');
testEventSource.addEventListener('test', (e) => {
const data = JSON.parse(e.data);
console.log(`count: ${data.count}`);
if (data.count >= 5) {
testEventSource.close();
}
});
});
<?php
// Set the event stream header(s).
header("Cache-Control: no-cache");
header("Content-Type: text/event-stream");
// XXX Override automatic detection of client abortion; we'll do it ourselves.
// (This was suggested in another answer, and I have the same issue with or without it)
ignore_user_abort(true);
// Initialize an arbitrary count parameter to investigate client communication.
$count = 1;
while ($count <= 10) {
$eventData = json_encode(array(
"count" => $count,
));
echo "event: test\n";
echo "data: ${eventData}";
echo "\n\n";
ob_flush();
flush();
$aborted = connection_aborted();
error_log("count: ${count}, connection_aborted: ${aborted}");
if ($aborted) {
break;
}
$count++;
sleep(1);
}
test
的 5 次发射。事件,然后停止看到
test
的任何进一步排放事件,但服务器继续执行完整计数 10,即使在
testEventSource.close()
之后也是如此在完整计数为 10 之前调用或关闭浏览器窗口,如此处的服务器日志内容所示:
count: 1, connection_aborted: 0
count: 2, connection_aborted: 0
count: 3, connection_aborted: 0
count: 4, connection_aborted: 0
count: 5, connection_aborted: 0
count: 6, connection_aborted: 0
count: 7, connection_aborted: 0
count: 8, connection_aborted: 0
count: 9, connection_aborted: 0
count: 10, connection_aborted: 0
最佳答案
根据更新的信息,我相信这绝对是错误的:
ignore_user_abort(true);
ignore_user_abort(false);
关于javascript - 为什么 EventSource 关闭时服务器端脚本不停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61274048/
我正在使用 https://github.com/streamdataio/eventsource-android/ 的 EventSource REPO我创建了一个 onConnect ();方法,
我正在开发一个由三个 Windows 服务和几个普通 Windows 应用程序 (.exe) 组成的大型产品。现在我们要转移到 ETW 和语义日志记录,并使用 Microsoft.Diagnostic
我正在研究使用 DDD+CQRS+EventSourcing 制作应用程序,但在弄清楚如何进行用户身份验证时遇到了一些麻烦。 用户本质上是我领域的一部分,因为他们对客户负责。我正在使用 ASP.NET
从 EventSource 开始API 我写的最学术的例子。 问题是我总是出错,我找不到任何有用的信息。当我加载 home.html , JS 脚本停在 source.onerror ;我将它打印到控
我不知道如何使用 JavaScript 从 EventSource 捕获事件。我正在这样做,但是“正在运行”永远不会出现在控制台中。 var es = new EventSource(
我创建了一个自托管 ServiceStack 服务,该服务基于其 showcase chat application 在 Windows 服务中运行。 。 但是,我没有进一步讨论的是尝试将客户端编写为
我正在使用 EventSource 登录到 ETW。我们正在使用 .Net Framework 4.5。 由于我处于开发的初级阶段,我将经常更改我的 EventSource 子类中的方法签名。我真的不
我正在使用 EventSource 在我的库中记录事件。该库是跨平台的,这意味着它可以被 linux/mac 用户使用。我知道 EventSource 在 Windows 上是如何工作的。用户可以使用
对 EventSource 行为有点困惑。 它连接良好并保持连接状态,直到收到第一条消息。 当我发送一条 data: something\n\n 消息时,连接断开并创建新连接。 使用 Content-
我在项目前端使用 JavaScript EventSource。 有时,浏览器和服务器之间的连接失败或服务器崩溃。在这些情况下,EventSource 会在 3 秒后尝试重新连接,如文档中所述。 但它
我正在尝试创建一个页面,该页面使用 EvenSource 对象(在 javascript 中)来创建服务器发送事件( cometd )。我阅读了很多关于它的教程,但没有找到可以解释以下问题的教程: 当
我正在尝试在我的新项目中使用 CQRS 和 EventSorcing。我正在遵循 Greg Young 几年前建议的方式(Mark Nijhof 实现 - http://cre8ivethought.
我有一个使用 EventSourcing 的 DDD 项目。目前有许多聚合根,其中许多都有实体的集合。甚至更多 - 一些实体具有其他实体的集合。 问题:出于审计目的读取 EventSourcing 事
我正在尝试为 Yesod 中的网站构建聊天功能,我希望它有一个所有已连接用户的列表。我认为最好的方法是将人员添加到连接上的用户列表中,并在他们离开时将其删除。如果保持连接打开,响应似乎应该阻塞,所以我
我有一个用例,我想发布一个非状态更改事件作为触发器。 In the vast majority of cases, the Aggregates will publish events by ap
我正在尝试使用 dddd 实现一个事件源系统。目前,我正在努力了解我的事件如何以及在何处跨越限界上下文的边界。 假设有两个限界上下文: 产品管理 物流系统 产品管理拥有关于产品的所有知识。为了简化,它
TL;DR 更新:无论是从客户端关闭 EventSource 还是完全关闭客户端,php 都会继续在后端执行并且无法报告 connection_aborted() 的正确值.为什么会这样? 我一直在
我正在尝试实现 Server Sent Events使用 Spring SseEmitter如本 Youtube video 中所述. 我能够启动事件流并从服务器发送的事件中接收数据。 但是,我可以看
我正在实现 FullCalendar 作为我的团队的假期日历。我希望团队中的每个成员的项目都有不同的颜色,因此我使用 ajax 请求从数据库中提取列表。我想知道如何将此列表组合到事件数组中以创建 ev
刚刚将我的 fullcalendar 更新到 1.6.3,将 jQuery 更新到 1.10.2突然我的日历不再显示来自“eventSources”的事件。我在文档中找不到任何说明需要更改的内容。为了
我是一名优秀的程序员,十分优秀!