有谁知道一种方法,或者可能认为可以将 Node.js 与 Nginx http 推送模块连接起来,以保持客户端和浏览器之间的持久连接。
我是彗星的新手,所以只是不了解出版等,也许有人可以帮助我。
到目前为止我设置的内容如下。我下载了 jQuery.comet 插件并设置了以下基本代码:
客户端 JavaScript
<script type="text/javascript">
function updateFeed(data) {
$('#time').text(data);
}
function catchAll(data, type) {
console.log(data);
console.log(type);
}
$.comet.connect('/broadcast/sub?channel=getIt');
$.comet.bind(updateFeed, 'feed');
$.comet.bind(catchAll);
$('#kill-button').click(function() {
$.comet.unbind(updateFeed, 'feed');
});
</script>
从中我可以理解的是,客户端将继续收听 url,然后是 /broadcast/sub=getIt。当有消息时,它将触发 updateFeed。
非常基本且易于理解的 IMO。
Nginx http 推送模块配置
default_type 应用程序/八位字节流;发送文件;keepalive_timeout 65; push_authorized_channels_only 关闭;
server {
listen 80;
location /broadcast {
location = /broadcast/sub {
set $push_channel_id $arg_channel;
push_subscriber;
push_subscriber_concurrency broadcast;
push_channel_group broadcast;
}
location = /broadcast/pub {
set $push_channel_id $arg_channel;
push_publisher;
push_min_message_buffer_length 5;
push_max_message_buffer_length 20;
push_message_timeout 5s;
push_channel_group broadcast;
}
}
}
好的,现在这告诉 nginx 在端口 80 上侦听对 /broadcast/sub 的任何调用,并将返回发送到 /broadcast/pub 的任何响应。
也很基本。这部分并不难理解,并且在互联网上有很好的记录。大多数时候,这背后都有一个 ruby 或 php 文件来进行广播。
我的想法是让 node.js 广播
/broadcast/pub
。我认为这会让我persistent streaming data
在不中断连接的情况下从服务器到客户端。我尝试了循环请求的长轮询方法,但我认为这会更有效。
或者这行不通。
Node.js 文件
现在创建 Node.js 我迷路了。首先,我不知道如何让 node.js 以这种方式工作。
我用于长轮询的设置如下:
var sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(new Date());
res.close();
seTimeout('',1000);
}).listen(8000);
这会侦听端口 8000 并仅写入响应变量。
对于长时间的轮询,我nginx.config
看起来像这样:
server {
listen 80;
server_name _;
location / {
proxy_pass http://mydomain.com:8080$request_uri;
include /etc/nginx/proxy.conf;
}
}
这只是将端口 80 重定向到 8000 并且工作正常。
有没有人知道如何让 Node.js 以 Comet 理解的方式运行。会非常好,你会帮我很多。
资源
用过的
要使用 faye,我必须安装 comet 客户端,但我想使用 Nginx 提供的客户端。这就是为什么我不只使用faye。nginx 使用的那个更加优化。
额外的