更新时间:2023-11-27 22:13:28
我签出了Socket.IO,但最终使用了Faye。 Faye可能比您想要的功能多一些,但是它很容易设置和使用。我发现它比Socket.IO更直观。并不是说Socket.IO看起来很艰难,但Faye看起来更轻松。
I checked out Socket.IO but ended up using Faye instead. Faye is probably a little more functionality than you want, but it's so easy to setup and use. I found it more intuitive than Socket.IO. Not that Socket.IO looks tough, but Faye just looked easier.
这里是网站:
http://faye.jcoglan.com/
也有一个google用户组,作者已经回答了我的一些查询。
There is a google users group too and the author has responded to a couple of my inquiries.
您需要的是以下代码:
节点端:
var faye = require('faye');
var faye_server = new faye.NodeAdapter({mount: '/faye', timeout: 120});
console.log('Firing up faye server. . . ');
faye_server.listen(8089);
//send message out every 1 second
setInterval( function()
{
var currentTime_secsSinceEpoch = new Date().getTime() / 1000;
faye_server.getClient().publish('/heartbeat',
{
pageName: 'app.js',
timeMessageSent_secs_since_epoch: currentTime_secsSinceEpoch,
iFrame1CycleCount: iFrame1CycleCount
});
}, 1000);
浏览器侧:
<script type='text/javascript'>
//create faye client
var faye_client = new Faye.Client('http://127.0.0.1:8089/faye');
var faye_message_subscription = faye_client.subscribe('/heartbeat', function(message)
{
//record the time message was received
var receiveTime_secSinceEpoch = new Date().getTime() / 1000;
console.log("Got heartbeat at " + receiveTime_secsSinceEpoch + "with a delay of " +
receiveTime_secsSinceEpoch - message.currentTime_secsSinceEpoch + " secs");
//Do something else important here!!
}
</script>
就是这样!现在,每当您的服务器发送心跳消息时,您的网页就会调用我们指定的函数,并向控制台吐出一条消息。
That's it! Now every time your server sends the 'heartbeat' message, your webpage will call the function we specified and spit out a message to the console. Dead simple.
Faye可能还需要使用许多其他很酷的功能,但这是我想要的。
Faye has a lot of other cool functionality you may want to use, but this is mostly what you will want I think.
您可以附加Faye真的很容易在节点***问快递服务器,这使得它可以与Web服务器一起很好地工作。
You can attach Faye to an express server in node too really easily which makes it work really well with a webserver.