最近在网上找到了一份php+ajax连实现comet的实例,但是我演示的时候只是实现了其中的一个客户端,怎么同时实现两个客户端之间的通信呢?

解决方案 »

  1.   

    如果代码不是很长的话,请你贴出来。这样便于讨论两个客户端之间的通讯需要通过服务端进行
    comet 总是下行的,客户端发出的信息需要通过另外的渠道
    这样,由 comet 触发的 php 程序并不能直接收到额外的用户信息。需要在服务端轮询公共资源获取
      

  2.   

    这是php代码
    <?php$filename  = dirname(__FILE__).'/data.txt';// store new message in the file
    $msg = isset($_GET['msg']) ? $_GET['msg'] : '';
    if ($msg != '')
    {
      file_put_contents($filename,$msg);
      die();
    }// infinite loop until the data file is not modified
    $lastmodif    = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
    $currentmodif = filemtime($filename);
    while ($currentmodif <= $lastmodif) // check if the data file has been modified
    {
      usleep(10000); // sleep 10ms to unload the CPU
      clearstatcache();
      $currentmodif = filemtime($filename);
    }// return a json array
    $response = array();
    $response['msg']       = file_get_contents($filename);
    $response['timestamp'] = $currentmodif;
    echo json_encode($response);
    flush();?>
      

  3.   

    这是html页面,引用了prototype
    <div id="content">
    </div><p>
      <form action="" method="get" onsubmit="comet.doRequest($('word').value);$('word').value='';return false;">
        <input type="text" name="word" id="word" value="" />
        <input type="submit" name="submit" value="Send" />
      </form>
    </p><script type="text/javascript">
    var Comet = Class.create();
    Comet.prototype = {  timestamp: 0,
      url: './backend.php',
      noerror: true,  initialize: function() { },  connect: function()
      {
        this.ajax = new Ajax.Request(this.url, {
          method: 'get',
          parameters: { 'timestamp' : this.timestamp },
          onSuccess: function(transport) {
            // handle the server response
            var response = transport.responseText.evalJSON();
            this.comet.timestamp = response['timestamp'];
            this.comet.handleResponse(response);
            this.comet.noerror = true;
          },
          onComplete: function(transport) {
            // send a new ajax request when this request is finished
            if (!this.comet.noerror)
              // if a connection problem occurs, try to reconnect each 5 seconds
              setTimeout(function(){ comet.connect() }, 5000); 
            else
              this.comet.connect();
            this.comet.noerror = false;
          }
        });
        this.ajax.comet = this;
      },  disconnect: function()
      {
      },  handleResponse: function(response)
      {
        $('content').innerHTML += '<div>' + response['msg'] + '</div>';
      },  doRequest: function(request)
      {
        new Ajax.Request(this.url, {
          method: 'get',
          parameters: { 'msg' : request }
        });
      }
    }
    var comet = new Comet();
    comet.connect();
    </script>
    我怎么感觉这两文件只能给单一的客户端发送信息?不能同时给两个用户发送求大神指点一二。。
      

  4.   

    多个的话你可以通过监听不同的文件标识来实现,例如AB两个客户端,分别创建两个监听文件A.txt、B.txt这样楼主明白了吗
      

  5.   

    我只知道Flex 的XMLSocket可以实现 所谓的服务器推。据说到时候HTML5也有类似功能了。
      

  6.   

    $filename = dirname(__FILE__).'/data.txt';
    你所有的连接都通过这个文件交换数据当然他是用 file_put_contents($filename,$msg); 来保存最后数据的
    这要的话就将其他连接产生的数据给覆盖了如果你给 file_put_contents 附加 FILE_APPEND 的话,就不会覆盖已有的数据
    从而达到信息共享的目的
    你只需有选择的发送其中的数据片段就可以了。这只是一个调度问题
      

  7.   

    如果所有的数据通过data.txt来处理的话,那我不是发一条数据,所有的用户都收到了吗?