node.js - 将数据推送到客户端 - 只能连接一个客户端?

发布于 2024-10-06 14:36:06 字数 669 浏览 0 评论 0原文

我正在尝试创建一个服务器端解决方案,通过 node.js 定期将数据推送到客户端(无客户端轮询)。连接应该永久打开,每当服务器有新数据时,它就会将其推送到客户端。

这是我的简单示例脚本:

var sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    sys.puts('Start sending...');

    setInterval(function(){
        res.write("<script type='text/javascript'>document.write('test<br>')</script>");
    }, 10000);
}).listen(8010);

这基本上可以工作,但似乎一次只能连接一个客户端。

如果我用浏览器打开 http://127.0.0.1:8010/ ,我会每 10 秒看到写入的新输出。但是当我打开另一个具有相同网址的选项卡时,它就会永远加载。仅当我关闭第一个选项卡时,我才能从服务器获取内容。

我需要做什么才能为多个客户端提供服务?

I am trying to create a server-side solution which periodically pushes data to the client (no client-side polling) via node.js. The connection should be open permanently and whenever the server has new data, it pushes it down to the client.

Here is my simple sample script:

var sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    sys.puts('Start sending...');

    setInterval(function(){
        res.write("<script type='text/javascript'>document.write('test<br>')</script>");
    }, 10000);
}).listen(8010);

This basically works, but it seems that only one client at a time can be connected.

If I open http://127.0.0.1:8010/ with my browser I see every 10 seconds the new output written. But when I open another tab with the same url, it just loads forever. Only if I close the first tab, I get conent from the server.

What do I need to do in order to server multiple clients?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

徒留西风 2024-10-13 14:36:07

这绝对是一个错误,所发生的情况是,由于 keep-alive 和 HTTP/1.1 和 Node 搞砸了,浏览器重新使用相同的连接。

您可以在 Opera11 中看到这一点,打开页面两次,这是完全相同的页面,使用完全相同的连接。

Curl 和所有未设置 Connection: keep-alive 的内容都可以正常工作,但浏览器无法两次打开同一页面。尽管您可以打开“localhost:8010”和“localhost:8010/foo”,但它将在两个页面上运行。

注意:这只影响GET请求,POST请求工作得很好,因为没有重新使用连接。

我已就此提交了一个问题

This is definitely a bug, what happens is that the Browser re-uses the same connection due to keep-alive and HTTP/1.1 and Node screws up.

You can see this at work in Opera11, open the page twice, it's the exact same page, using the exact same connection.

Curl and everything that doesn't set Connection: keep-alive works just fine, but Browsers fail to open the same page twice. Although you can open 'localhost:8010' and 'localhost:8010/foo' and it will work on both pages.

Note: This only affects GET requests, POST requests work just fine since there's no re-using of the connection.

I've filed an issue on this.

东风软 2024-10-13 14:36:07

您应该使用 socket.io。它为您处理所有繁重的工作,是一个非常酷的库。

You should use socket.io. It handles all the heavy lifting for you and is a really cool library.

丿*梦醉红颜 2024-10-13 14:36:07

小心这个!

node.js 非阻塞的,但同时一次仅处理 1 个连接。您所做的是将节点置于死亡状态,这就是为什么当您关闭第一个客户端时您会在第二个客户端上看到数据。

Be careful with this!

node.js is non-blocking but at the same time handles only 1 connection at a time. What you did is put the node into a dead state, that's why you see data on the second client when you close the first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文