我想构建一个实时仪表板,在该仪表板上,只要有任何更新,脚本就可以轮询并将更新推送到仪表板。任何人都可以告诉你这样做的最佳方法是什么。 node.js会有所帮助吗?
最佳答案
更新
截至2013年8月10日为止,由于诸如mean.io之类的库,缺少node.js的库和支持的观点实际上已经消失了,实际上您可以在数小时或数小时内使自己运转起来。
如以上两个答复所述,Anush确实提供了很多信息,您为服务器端提供了两种解决方案。
1. Node.js Based 'Event based' server
2. PHP/ASP.net based polling/comet/forever script based server
据我所知和经验,Node.js和Socket.io具有很大的优势,因为它的简单性和实时Web开发应用的目的
,但是“Php具有稳定的强大功能”,因此这里是上述两个框架的一些优点和缺点。
PHP:
优点:
用于RealTime API的PHP机制具有缺点DrawBacks
如果您不需要非常出色的实时api及其最好的语言来编写动态网页,则PHP是不错的选择,但是在编写更快的应用程序时,PHP会回退,因为它阻塞了服务器并占用了更多资源。
现在是第二个解决方案。
Node.js
Node是一个基于事件的服务器软件,它仅在事件发生时才起作用,例如Apache或PHP启动时它们就来了,并告诉主机OS它们将处理特定端口(例如端口80)上的请求,现在它们不断留在内存中,并且即使不使用也要消耗资源。就像在Comet/Forever Open Connections中一样,从数据库中获取数据的内部轮询会永远保持一个开放的APACHE线程永远占用您的资源,但是在Node的情况下,应用程序启动node.js就会起作用,并告诉OS何时知道在特定端口上发出请求后,当请求到达时,它进入休眠状态(即不执行任何操作并让操作系统处理该请求),node.js完成该请求,然后再次进入休眠状态,这意味着它可以在需要时继续工作在其他情况下,内存保持可用,CPU等资源不足。
Node.js相当新,有时也很不稳定,但是如果写得不错,它可以惊人地提高应用程序的性能。它具有良好的支持,您可能需要访问chat.stackoverflow.com上的javascript聊天室,以使用socket.io作为后端在node.js中获得帮助。
socket.io的作用是它允许程序员仅编写他/她的应用程序,而不能编写通信所需的基本内容,它可以自动处理订单中的运输方法
这样做可以确保您的程序在所有Web浏览器和Node.js上以与服务器相同的速度和质量运行。
当您学习node.js时,您将了解编写在php上非常困难的东西是多么容易。例如说在php上永远打开代码,以便从数据库中获取数据时向用户发送数据(现在说mysql)
<?php
ob_implicit_flush(true);
set_timeout(0);
$T = 0; // for the first run send all the events
while(true){
$query = mysql_query("select * from database where TimeStamp > ".$T);
if(mysql_num_rows(query)>0){ // That is omg we have an updated thing
$T = microtime(true); // this gives the variable the value of current time stamp
while ($row = mysql_fetch_assoc($query))
{ $result = process($row) // say u have to do some processing on the row
echo json_encode($result); // this will send the JSON formation to your client for faster processing
}
}
}
?>
现在,您还将需要一些东西来管理对数据库的输入,依此类推,因此还有1个.php格式的文件可以接受输入。
取而代之的是,用socket.io编写的node.js代码看起来像
// Assume all functions are declared.
var io = require("socket.io").listen(82);
io.sockets.on('connection',function(client){
client.on('authorise',function(info){
var signin = Authorise(info); // say the info packet contains information about client user pwd and the Authorise function checks for the data to be true or false and then acts accordingly returns a bool status which is true on success with autorisation_id(a member variablE)for the client and false on failure with the reason on failure in the reason member variable inside the signin object.
socket.emit('authorised',signin);
});
client.on('request',function(data){
var result = process(data);
client.emit('reply',result); // yes you can also send straight javascript objects
});
client.on('some_other_event', function(ev){ // SOmething happend such as some long task completed or some other client send a message that can be sent this way.
client.emit('event',ev);
});
.
.
// and so on
});
On client side a
<script src="server:82/socket.io/socket.io.js"></scirpt>
<script>
var connection = io.connect("server:82");
connection.on('event',function(data){
Process_Event(data); // some function which processes event data
});
connection.on('authorised',function(data){
Auth(data); // Some function which tells client hey you are authroised or not and why not
});
connection.on('reply',function(data){
parse_reply(data); // some function that tells the client what is the reply of the request by server and what it has to do
});
function Authorise(){ // Lets assume its called by some html event
username = document.getElementById('username').value;
password = document.getElementById('password').value;
socket.emit('authorise',{usr:username,pass:password}); // sends the request to server
}
function Request(req) {} // this is a function which is called by some script gives it the request as the parameter when calling
{
socket.emit('request',req); // sends request to server
}
</script>
如果尚未注意到,则后者中没有for/while(true)循环,因此它不会以任何方式阻塞服务器,这是在php Polling,Comet或BSD Sockets上将node.js与socket.io结合使用的基本方法。
现在,好选择就是您的了!选择明智的:)取决于您的应用程序,随时可以在chat.stackoverflow.com或此处提出更多疑问:)
关于jquery - 从服务器推送更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6935911/