jquery - 从服务器推送更新

标签 jquery node.js socket.io

我想构建一个实时仪表板,在该仪表板上,只要有任何更新,脚本就可以轮询并将更新推送到仪表板。任何人都可以告诉你这样做的最佳方法是什么。 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:
优点:
  • 稳定性
  • 电源
  • 如果您熟悉C++(例如后端
  • ),则易于编程
  • 一个庞大的在线支持库(php.net)

  • 用于RealTime API的PHP机制具有缺点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,可以随时查看源代码并在控制台/firebug中对其进行检查。但是当客户端上有大约8个人时,服务器崩溃。
  • Php Sockets(这是内置于php中的1镜头BSD套接字解决方案),这里的问题是您最多将绑定(bind)到几百个用户。否则,这是最好的解决方案。因为它为您提供了php的“功能”和“稳定性”,并且易于与网络套接字一起使用。对于客户端,您可以使用免费的分布式XMLsocket API ,但是此类套接字最好用Java或C++编写。

  • 如果您不需要非常出色的实时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套接字
  • Flash套接字。
  • JsonP轮询
  • XHR轮询

  • 这样做可以确保您的程序在所有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/

    相关文章:

    javascript - 确保 jQuery 事件处理程序执行顺序

    jquery - 使用jquery格式化表单中的电话号码

    node.js - 使用 Node.JS 访问数据库 informix

    node.js - 如何使用带有 promise 的异步等待来获取结果?

    javascript - Socket.io 与 PubSub : Less to no real-time data displayed after multiple page refresh

    javascript - jQuery fadeIn 动画对 Ajax 加载有影响吗?

    javascript - 单击淡出不起作用

    javascript - 等待递归 readdir 函数结束

    ios - 无法将类型 'NSURL' 的值转换为 'SocketIO.SocketManagerSpec

    node.js - 如何关闭 socket.io 1.0 中的服务器?