更新 截至2013年10月8日:缺少node.js的库和支持点大部分已经消失了,因为像mean.io这样的库你实际上可以让你自己在一个或几个小时内运行起来.
Anush在上面的两个回复中说明了真正有用的信息,你有2个服务器端的解决方案.
1. Node.js Based 'Event based' server
2. PHP/ASP.net based polling/comet/forever script based server
Run Code Online (Sandbox Code Playgroud)
尽我所知和经验Node.js和Socket.io有一个非常大的优势,因为它的简单性和实时网络应用程序的开发目的,但然后"Php稳定强大"所以这里有几个上述框架的优点和缺点.
PHP: 优点:
稳定性
功率
如果你喜欢像后端这样的C++,那么易于编程
一个庞大的在线图书馆(php.net)
PHP for RealTime API的机制有缺点DrawBacks
通常,在使用长轮询方法时,服务器必须每秒处理来自同一用户的n个请求.这1个请求包含8KB的请求数据+ cookie,用于发送到服务器以获取信息.这会增加服务器开销,并且您可能还希望每次都授权客户端(为了更好的安全性),因此授权的1个功能将被调用n次,这实际上增加了服务器负载.低于n你会失去史诗般的实时.如果你有更高的n,你可以在4小时内为21个人创建一百万个请求.
使用Comet时,您必须保持请求打开一段时间,并不断轮询数据库或服务器API以获取仪表板中的更改.由于没有打开APACHE/IIS线程,这会阻塞整个脚本并反复增加服务器负载.(Comet依赖Keep Alive Server),如果与强大的服务器一起使用,它的特殊解决方案,因此根据您的服务器做出明智的选择.
当使用AJAX的永久打开请求时.在那里你连续打开1个请求并永远保持打开以监听服务器端的推送,你会再次使服务器过载,如案例2所示.我完全不推荐这个我在http://chats.ws/上写了这样的applet 随时看到源代码并在console/firebug中检查它.但是当客户端上有8个人时,服务器崩溃了.
Php套接字(这是一个内置于php的1枪BSD套接字解决方案),这里的问题是你最多只能绑定几百个用户.这是最好的解决方案.否则这将为您提供php的"强大"和"稳定性",并且易于使用Web套接字.对于客户端,您可以使用免费的分布式XMLsocket API.但是这样的样式套接字更好地用Java或C++编写.
PHP是一个很好的选择,如果您不需要非常棒的实时api和它是编写动态网页的最佳语言,但是当编写更快的应用程序时,PHP会因为它阻塞服务器并占用更多资源而退回.
现在来了第二个解决方案.
Node.js Node是一个基于事件的服务器软件,意味着它仅在事件发生时起作用,例如当Apache或PHP启动它们时告诉主机操作系统它们将处理特定端口上的请求,即端口80现在它们会持续停留在记忆中,即使不使用也会吃掉资源.就像在comet/Forever Open Connections中一样,从数据库中获取数据的内部轮询使得一个开放的APACHE线程永远占用你的资源,但是在Node的情况下,应用程序启动了node.js,它告诉操作系统让它知道何时当一个请求到达node.js完成请求并再次进入睡眠状态时,它会在特定端口上发出请求然后进入休眠状态(即不执行任何操作并让操作系统处理它),这意味着它在需要时启用在其他情况下,内存保持空闲,CPU使用率等资源也很少.
Node.js相当新颖且有时非常不稳定,但如果编写得很好,它会极大地提升应用程序的性能.它有一个很好的支持,你可能想访问chat.stackoverflow.com上的javascript聊天室,以获得node.js与socket.io作为后端的帮助.
socket.io所做的是它允许程序员只编写他/她的应用程序,而不是通信所需的基本内容,它会自动处理订单中的运输方法
这样做可以确保您的程序在所有Web浏览器和Node.js上以与服务器相同的速度和质量运行.
当你学习node.js时,你会明白在php上编写非常难的东西是多么容易.例如说在php上永远打开代码,以便在从数据库获取availibe时向用户发送数据(现在说是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
}
}
}
?>
Run Code Online (Sandbox Code Playgroud)
现在你还需要一些东西来管理数据库的输入等等,所以还有一个.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>
Run Code Online (Sandbox Code Playgroud)
如果你还没有注意到后者中没有for/while(true)循环,因此它不会以任何方式阻止服务器这是使用带有socket.io的node.js而不是php Polling,Comet或BSD套接字的基本上手.
现在你的选择很好!选择明智:)因为您的应用程序取决于它随时可以在chat.stackoverflow.com或在这里问更多的疑问:)
| 归档时间: |
|
| 查看次数: |
5319 次 |
| 最近记录: |