Ajax轮询

时间:2020-03-05 18:45:17  来源:igfitidea点击:

在我目前正在进行的项目中,我们需要开发一个Web聊天应用程序,而不是一个非常复杂的聊天,仅是一种将两个人联系起来谈论一个非常具体的主题的方式,我们不需要任何身份验证对于这两个用户之一,我们不必支持表情符号,头像或者类似的东西。

一些项目成员建议我们可以通过BOSH使用XMPP,我说这就像试图用船网抓鱼,并提出了一种更简单的方法,例如简单的Ajax / MySQL网络聊天,但我们对此感到担心。由于对同时打开的许多聊天进行持续轮询,因此服务器的性能受到了影响。

有人做过这样的事吗?你会推荐什么?

解决方案

回答

What would you recommend?

通过BOSH的XMPP

当其他人拥有时,无需发明自己的消息格式和传输协议。如果尝试使用,它将慢慢增长到与BOSH一样复杂,但是没有第三方库支持或者标准化的好处。

回答

我们可能还想研究彗星。

GTalk,Meebo和许多其他聊天应用程序都使用它。几年前,当我进行实验时,并没有太多关于服务器架构实现的库或者细节,但是现在看来还有更多的东西。

请查看Cometd项目以获取更多技术信息。

回答

我以为每个人都用彗星做这种事情。

回答

You might also want to look into Comet.
  
  I thought everyone used cometd for this sort of thing.

BOSH是通过HTTP传输XMPP的标准。它涉及Comet将数据推送到客户端。

回答

几个月前,我做了同样的事情,并且在玩弄这些概念的过程中获得了乐趣。实际上,我使用了永远帧技术而不是轮询。

下面的代码是我的" comet" js文件,其中包含获得"聚会聊天"设置所需的一般概念。

function Comet(key) {

  var random = key;
  var title = 'Comet';
  var connection = false;
  var iframediv = false;
  var browserIsIE = /*@cc_on!@*/false;
  var blurStatus = false;
  var tmpframe = document.createElement('iframe');
  var nl = '\r\n';

  this.initialize = function() {
    if (browserIsIE) {
      connection = new ActiveXObject("htmlfile");
      connection.open();
      connection.write("<html>");
      connection.write("<script>document.domain = '"+document.domain+"'");
      connection.write("</html>");
      connection.close();
      iframediv = connection.createElement("div");
      connection.appendChild(iframediv);
      connection.parentWindow.comet = comet;
      iframediv.innerHTML = "<iframe id='comet_iframe' src='./comet.aspx?key="+random+"'></iframe>";
    } else {
      connection = document.createElement('iframe');
      connection.setAttribute('id', 'comet_iframe');
      iframediv = document.createElement('iframe');
      iframediv.setAttribute('src', './comet.aspx?key='+random);
      connection.appendChild(iframediv);
      document.body.appendChild(connection);
    }
  }

  // this function is called from the server to keep the connection alive
  this.keepAlive = function () {
    if (!browserIsIE) {
        mozillaHack();
    }
  }

  // this function is called from the server to update the client
  this.updateClient = function (value) {
    var outputDiv = document.getElementById('output');
    outputDiv.value = value + nl + outputDiv.value;
    if (blurStatus == true) {
        document.title = value;
    }
    if (!browserIsIE) {
        mozillaHack();
    }
  }

  this.onUnload = function() {
    if (connection) {
      // this will release the iframe to prevent problems with IE when reloading the page
      connection = false;
    }
  }

  this.toggleBlurStatus = function(bool) {
    blurStatus = bool;
  }

  this.resetTitle = function() {
    document.title = title;
  }

  function mozillaHack() {
    // this hack will fix the hour glass and loading status for Mozilla browsers
    document.body.appendChild(tmpframe);
    document.body.removeChild(tmpframe);
  }
}

回答

我同意约翰的观点。但是还有另一个问题没有得到回答。
我已经做到了,但是它没有使用数据库,而是使用了平面文件,它最终使服务器瘫痪了,但是直到我们有约450个活跃用户时才这样做,如果我们对数据库进行操作,它可能会失败了。更好的。
这是通过Godaddy的基本托管帐户完成的。

编辑:BTW哥达迪听起来没那么有趣,当我接到电话时。

回答

我认为轮询是最简单的方法,建议先这样做。如果负载成为问题,那就开始研究更复杂的技术。
关于优缺点的很好的讨论在这里
http://www.infoq.com/news/2007/07/pushvspull
http://ajaxian.com/archives/a-report-on-push-versus-pull

回答

有一个非常好的服务器,用于处理从服务器到浏览器(称为Comet)的消息推送。就像memcached一样,它很容易与其他技术(Django,Rails,PHP等)集成。

如果我们要处理严重的负载,则确实应该检查它。否则,简单的Ajax轮询是最好的方法。

回答

诀窍是要认识到应用程序唯一需要在服务器上调用CGI的时间是有人说出什么时候。对于常规轮询,请轮询一个静态页面,当有新聊天记录时,CGI脚本会更新该页面。使用HEAD请求,将时间戳与上次查看的时间戳进行比较,并且仅在更改时执行完整的GET。我有一个以这种方式实现的简单天真的聊天应用程序,对于我们拥有的数十个同时用户,负载和带宽使用情况可以忽略不计。

回答

如果我们不喜欢HTTP轮询的想法,则可以在聊天页面上创建一个Flash电影,该电影与服务器上的某些守护进程保持恒定的连接,然后,该Flash电影将在客户端上调用JavaScript函数进行更新随着新消息的出现聊天。 (除非我们需要一个用于聊天的Flash界面。)

回答

结帐Speeqe。它是基于Web的聊天室的开源解决方案,在后台使用BOSH和XMPP。