需要使用node.js将简单的数据推送到浏览器

在服务器端,我使用node.js来做一些分布式的asynchronous乒乓。 我现在需要在客户端浏览器中将结果显示为实时图表。 为了简单起见,我正在使用基于图像的Google图表URL,并限制要绘制的数据量。 最终这个客户端显示片将是丰富和互动的。

我明白,我的服务器将数据推送到浏览器的方式之一是彗星。 我期望在浏览器端必须有一个相应的套接字,所以两者应该在一起。

Q1:对于原型devise:将string数据从node.js推送到我的Firefox 3.6.10浏览器最简单的方法是什么? string每秒更新less于1KB一次。

Q2:对于生产:对于可以在多种浏览器(包括移动设备)上使用的方法的build议? 二进制更新顺序为每秒100KB,无图像或video。

我真的build议看看http://socket.io/的Node.js。 它可以在移动设备上运行,并且支持您希望使用浏览器可用的最佳选项的多种Comet效果方法。

虽然它没有频道,但它很简单,但是使用socket.broadcast(msg, [array containing every user except those 'subscribed'])是一个简单的解决方法socket.broadcast(msg, [array containing every user except those 'subscribed'])

每两秒服务器在[0,100]中生成一个随机数r1,然后消息客户端用r1和r2 = 100-r1绘制一个饼图。 然而为了实现为多个客户build议的广播。 任何其他的改进build议欢迎。

服务器端(在coffeescript):

 http = require('http') io = require('socket.io') server = http.createServer( ) server.listen(8000) socket = io.listen(server) myrand = (client) -> setInterval( -> r1 = Math.floor(Math.random()*101) r2 = 100-r1 client.send(String(r1) + ',' + String(r2)) , 2000) socket.on('connection', (client) -> myrand(client)) 

客户端(带javascript的index.html):

 <h1>My socket client</h1> <script src="http://cdn.socket.io/stable/socket.io.js"></script> <div id="piechart"> Hello World </div> <script> socket = new io.Socket('localhost:8000'); socket.connect(); socket.on('message', function(data){ url = 'http://chart.apis.google.com/chart?cht=p3&chs=250x100&chd=t:' + data + '&chl=Hello|World'; document.getElementById('piechart').innerHTML = "<img src="+ url + "></img>"; }); </script>