Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 聊天应用程序AJAX轮询_Php_Ajax_Chat - Fatal编程技术网

Php 聊天应用程序AJAX轮询

Php 聊天应用程序AJAX轮询,php,ajax,chat,Php,Ajax,Chat,在我目前正在进行的项目中,我们需要开发一个网络聊天应用程序,而不是一个非常复杂的聊天,只是一种连接两个人谈论一个非常具体的话题的方式,我们不需要对两个用户中的一个进行任何形式的身份验证,我们不需要支持表情符号、化身或类似的东西 一些项目成员建议我们可以通过BOSH使用XMPP,我说这就像试图用船上的网捕鱼一样,并提出了一种更简单的方法,比如简单的Ajax/MySQL网络聊天,但我们担心服务器的性能会受到影响,因为同时打开的许多聊天会不断轮询 以前有人做过这样的事吗?你推荐什么 你推荐什么 XMP

在我目前正在进行的项目中,我们需要开发一个网络聊天应用程序,而不是一个非常复杂的聊天,只是一种连接两个人谈论一个非常具体的话题的方式,我们不需要对两个用户中的一个进行任何形式的身份验证,我们不需要支持表情符号、化身或类似的东西

一些项目成员建议我们可以通过BOSH使用XMPP,我说这就像试图用船上的网捕鱼一样,并提出了一种更简单的方法,比如简单的Ajax/MySQL网络聊天,但我们担心服务器的性能会受到影响,因为同时打开的许多聊天会不断轮询

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

你推荐什么

XMPP通过波什


当别人有消息时,没有必要发明自己的消息格式和传输协议。如果您尝试,它将慢慢变得像波什一样复杂,但没有第三方库支持或标准化的好处。

您可能还想研究一下

GTalk、Meebo和许多人都使用它。几年前,当我尝试它时,没有太多的库或关于服务器体系结构的细节来实现它,但现在看起来有更多的东西


更多技术信息,请查看该项目。

我认为每个人都使用cometd来完成这类工作


你们可能还想看看彗星


我以为每个人都用cometd来做这类事情


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

几个月前,我做了同样的事情,只是在玩弄这些概念。我实际上使用了这种技术而不是轮询

下面的代码是我的“comet”js文件,其中包含获得“party chat”设置所需的一般概念

函数Comet(键){
var随机=键;
var title='Comet';
var连接=错误;
var-iframediv=false;
var browserIsIE=/*@cc_on!@*/false;
var-status=false;
var tmpframe=document.createElement('iframe');
变量nl='\r\n';
this.initialize=函数(){
如果(布朗语){
连接=新的ActiveXObject(“htmlfile”);
connection.open();
连接。写(“”);
connection.write(“document.domain=”+“document.domain+”);
连接。写(“”);
connection.close();
iframediv=connection.createElement(“div”);
连接.appendChild(iframediv);
connection.parentWindow.comet=comet;
iframediv.innerHTML=“”;
}否则{
连接=document.createElement('iframe');
connection.setAttribute('id','comet_iframe');
iframediv=document.createElement('iframe');
iframediv.setAttribute('src','./comet.aspx?key='+random);
连接.appendChild(iframediv);
document.body.appendChild(连接);
}
}
//从服务器调用此函数以保持连接处于活动状态
this.keepAlive=函数(){
如果(!browserIsIE){
mozillaHack();
}
}
//从服务器调用此函数以更新客户端
this.updateClient=函数(值){
var outputDiv=document.getElementById('output');
outputDiv.value=value+nl+outputDiv.value;
如果(模糊状态==真){
document.title=值;
}
如果(!browserIsIE){
mozillaHack();
}
}
this.onUnload=函数(){
如果(连接){
//这将释放iframe,以防止重新加载页面时IE出现问题
连接=错误;
}
}
this.toggleBlurStatus=函数(bool){
模糊状态=布尔;
}
this.resetTitle=函数(){
document.title=标题;
}
函数mozillaHack(){
//此黑客将修复Mozilla浏览器的计时器和加载状态
document.body.appendChild(tmpframe);
document.body.removeChild(tmpframe);
}
}

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


编辑:顺便说一句,当我接到电话时,Godaddy听起来不那么好笑。

我认为投票是最简单的方法,我建议先这样做。如果负载成为问题,开始研究更复杂的技术。 关于利弊的详细讨论如下:

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


如果你想处理严重的负载,你真的应该检查一下。否则,简单的Ajax轮询是最好的方法。

诀窍是要意识到,应用程序在服务器上调用CGI的唯一时间是有人说话的时候。对于常规轮询,轮询一个静态页面,每当有新聊天时,CGI脚本都会更新该页面。使用HEAD请求,将时间戳与上次看到的时间戳进行比较,并且仅在这些时间戳发生变化时执行完整GET。我用这种方式实现了一个简单的朴素聊天应用程序,对于我们同时拥有的几十个用户来说,负载和带宽使用可以忽略不计。

如果你不喜欢HTTP轮询的想法,你可以在聊天页面上有一个Flash电影,它可以与服务器上的某个deamon保持恒定的连接,Flash电影随后会调用客户端上的JavaScript函数,在出现新消息时更新聊天。(除非您希望聊天时使用Flash界面。)

结帐。这是一个基于网络的聊天室,在幕后使用BOSH和XMPP。

我刚刚发现这篇文章,它很旧,但投票概念给很多人带来了麻烦。所以我很高兴
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);
  }
}
<?php

    // For this demo
    if (file_exists('poll.txt') == false) {
        file_put_contents('poll.txt', '');
    }

    if (isset($_GET['poll'])) {

        // Don't forget to change the default time limit
        set_time_limit(120);

        date_default_timezone_set('Europe/Paris');
        $time = time();

        // We loop until you click on the "release" button...
        $poll = true;
        $number_of_tries = 1;
        while ($poll)
        {
            // Here we simulate a request (last mtime of file could be a creation/update_date field on a base)
            clearstatcache();
            $mtime = filemtime('poll.txt');

            if ($mtime > $time) {
                $result = htmlentities(file_get_contents('poll.txt'));
                $poll = false;
            }

            // Of course, else your polling will kill your resources!
            $number_of_tries++;
            sleep(1);
        }

        // Outputs result
        echo "Number of tries : {$number_of_tries}<br/>{$result}";
        die();
    }

    // Here we catch the release form
    if (isset($_GET['release']))
    {
        $data = '';
        if (isset($_GET['data'])) {
            $data = $_GET['data'];
        }
        file_put_contents('poll.txt', $data);
        die();
    }

?>

<!-- click this button to begin long-polling -->
<input id="poll" type="button" value="Click me to start polling" />

<br/><br/>

Give me some text here :
<br/>
<input id="data" type="text" />
<br/>

<!-- click this button to release long-polling -->
<input id="release" type="button" value="Click me to release polling" disabled="disabled" />

<br/><br/>

Result after releasing polling :
<div id="result"></div>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">

// Script to launch polling
$('#poll').click(function() {
    $('#poll').attr('disabled', 'disabled');
    $('#release').removeAttr('disabled');
    $.ajax({
        url: 'poll.php',
        data: {
            poll: 'yes' // sets our $_GET['poll']
        },
        success: function(data) {
            $('#result').html(data);
            $('#poll').removeAttr('disabled');
            $('#release').attr('disabled', 'disabled');
        }
    });
});

// Script to release polling
$('#release').click(function() {
    $.ajax({
        url: 'poll.php',
        data: {
            release: 'yes', // sets our $_GET['release']
            data: $('#data').val() // sets our $_GET['data']
        }
    });
});

</script>