- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有以下 Node 应用程序
var express = require("express"),
app = express();
app.get("/api/time", function(req, res) {
sendSSE(req, res);
});
function sendSSE(req, res) {
res.set({
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Access-Control-Allow-Origin": "*"
});
var id = (new Date()).toLocaleTimeString();
setInterval(function() {
constructSSE(res, id, (new Date()).toLocaleTimeString());
}, 5000);
constructSSE(res, id, (new Date()).toLocaleTimeString());
};
function constructSSE(res, id, data) {
res.write("id: " + id + "\n");
res.write("data: " + data + "\n\n");
}
var server = app.listen(8081, function() {
});
我正在使用它在我的客户端应用程序中使用服务器端事件。当我浏览到 http://localhost:8081/api/time它立即开始返回。如果我在另一个浏览器窗口中打开 URI,那么它需要几秒钟才能响应,但是它可以正常工作。
所以我的问题是 setInterval 阻塞,或者对于性能不佳还有其他解释吗?基于this answer它不应该是,但我不认为 constructSSE
会花费 5 秒。但是,我发现了一个问题。
谢谢。
更新根据建议,它可能与 express
有关,我将其删除并仅使用 http
模块。
var http = require("http");
http.createServer(function(req, res){
if (req.url == "/api/time") {
sendSSE(req, res);
}
}).listen(8081);
function sendSSE(req, res) {
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Access-Control-Allow-Origin": "*"
});
var id = (new Date()).toLocaleTimeString();
setInterval(function() {
constructSSE(res, id, (new Date()).toLocaleTimeString());
}, 5000);
constructSSE(res, id, (new Date()).toLocaleTimeString());
};
function constructSSE(res, id, data) {
res.write("id: " + id + "\n");
res.write("data: " + data + "\n\n");
};
它具有完全相同的行为。所以它看起来像是 Node 的一些限制,或者是我的错误。
最佳答案
我认为性能不佳的原因与 Node 或 setInterval 本身无关,而是与您读取事件数据的方式有关。我进行了一些搜索,并实现了 3 或 4 个在 Node 上的服务器发送事件网络上找到的不同示例,它们都遇到了同样的问题。
我不认为 node.js 不适合这项任务。
我试过了
我正要开始使用 webworker-threads 进行测试或 cluster module或其他服务器平台试图确定问题,然后我想到编写一个小页面来使用 EventSource API 获取事件。当我这样做时,一切正常,此外,在之前使用 Chrome 的测试中,我看到在网络选项卡中,SSE 请求包含一个名为 EventStream 的附加选项卡,但内容为空,即使数据定期到达。
这让我相信可能是浏览器没有以错误的方式正确解释请求,因为它是使用地址栏而不是 EventSource API 请求的。我不知道这是什么原因,但我举了一个小例子,绝对没有表现不佳。
我这样修改了你的代码。
添加了另一条到网站根目录的路由以进行测试
var express = require("express"),
app = express();
// Send an html file for testing
app.get("/", function (req, res, next) {
res.setHeader('content-type', 'text/html')
fs.readFile('./index.html', function(err, data) {
res.send(data);
});
});
app.get("/api/time", function(req, res) {
sendSSE(req, res);
});
创建了一个 index.html 文件
<head>
<title>Server-Sent Events Demo</title>
<meta charset="UTF-8" />
<script>
document.addEventListener("DOMContentLoaded", function () {
var es = new EventSource('/api/time'),
closed = false,
ul = document.querySelector('ul');
es.onmessage = function (ev) {
var li;
if (closed) return;
li = document.createElement("li");
li.innerHTML = "message: " + ev.data;
ul.appendChild(li);
};
es.addEventListener('end', function () {
es.close()
closed = true
}, true);
es.onerror = function (e) {
closed = true
};
}, false);
</script>
</head>
<body>
<ul>
</ul>
</body>
{编辑}
我还想指出,感谢@Rodrigo Medeiros,使用 curl
向 /api/time
发出请求并没有表现不佳,这强化了以下观点是浏览器相关的问题。
关于javascript - setInterval 是否阻塞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31031922/
这个问题已经有答案了: JavaScript closure inside loops – simple practical example (45 个回答) 已关闭 8 年前。 我试图创建多个 se
这是我的情况,我需要加快函数运行时间,所以 setInterval 不是一个明智的选择,对吧?因为每次至少要花费 4 毫秒。 所以,我可以将 setInterval 函数更改为 requestAnim
我正在尝试下面的代码,看看是否可以将 setInterval Id 存储为关联数组中唯一键的值,然后通过使用唯一值作为键来停止被调用函数中的间隔,如下所示我将 setInterval Id 作为它的值
我花了很长时间试图弄清楚如何使用具有 CSS 样式缓动功能的 Google Maps API 为折线上的符号设置动画。我让它工作 with this Gist和 this Google Maps AP
我是这里的 JS 新手,正在研究一个在给定时间段后开始的倒数计时器。基本上,当用户单击按钮时时钟开始,第二个时钟在第一个计时器用完时开始。我知道“setInterval”是完成这个的最好方法。我面临的
嗨, friend 们,我想只使用一个 setInterval 函数来运行我的代码。目前我正在使用两个 setInterval's 来实现我的目标,我们可以只使用一个 'setInterval' 来获
我的“setInterval”函数有问题,我想在将鼠标悬停在 div 上时启动该函数,但是 setInterval 在我将鼠标悬停在 div 上时第一次起作用=>如果我停留在div,它不会继续改变图片
我想展示两次:一次在你的国家,一次在日本用JS 问题是第二个 setInterval 停止了第一个,我不知道如何进行两次运行。 完整代码 In your region:
好吧,这个问题有几个问题。 首先,我要求 setTimeout() 和 setInterval() 我见过几种不同的调用方式,我想知道哪种方式最适合这种情况。 我正在制作一个 js/canvas 游戏
setInterval(function () { //======= //code //======== if(--timer&etype="; } },1000); 这里定时器结束后,而不是重定
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 5 年前。 Improv
我的前老板有一个 weird bug where when he used setInterval with a long delay interval : setInterval(func, 300
这个问题已经有答案了: How does the "this" keyword work, and when should it be used? (22 个回答) How to access the
我的印象是 setInterval("/*some code*/", time) 相当于 setInterval(function() { /*some code*/ }, time) 显然不是
我对 JavaScript 和 NodeJS 非常陌生,我只是想了解 NodeJS 中的发射器模式。当我尝试使用 setInterval 函数每秒发出一个刻度事件时,程序似乎工作正常:-
我有一个简单的 setTimeout 函数,它在特定时间运行并且工作正常: var now = new Date(); var milliTillExec = new Date(now.getFull
在本教程中,您将借助示例了解 JavaScript setInterval() 方法。 在 JavaScript 中,可以在指定的时间间隔内执行一段代码。这些时间间隔称为定时事件。 有
Js: function cometConnect(){ $.ajax({ cache:false, type:"post", d
LE2。关于如何解决此问题的任何其他想法? 我有这段代码,但不知道为什么不能正常工作: $(function autorun() { if ($("#contactForm").is(":visibl
这个问题在这里已经有了答案: How to make a setInterval stop after some time or after a number of actions? (6 个答案)
我是一名优秀的程序员,十分优秀!