Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.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
反向AJAX PHP多用户聊天_Php_Jquery_Mysql_Ajax_Reverse Ajax - Fatal编程技术网

反向AJAX PHP多用户聊天

反向AJAX PHP多用户聊天,php,jquery,mysql,ajax,reverse-ajax,Php,Jquery,Mysql,Ajax,Reverse Ajax,情况是这样的。我有一个多个管理员同时使用的管理面板和多个员工同时使用的员工面板。管理员可以同时与多个员工聊天,但员工只能与管理员聊天。所有聊天都保存在databasemysql中,并带有各自的from、to、msg和time字段,双方都使用这些字段根据其活动聊天会话匹配消息。 现在我正在使用 setIntervalfunction{$.ajax…;},3000;作为回报,服务器将所有消息返回给管理员,但只返回请求员工的特定消息。但这不是一个实用的解决方案,因为如果它不正确的话,它也会用过多的数据

情况是这样的。我有一个多个管理员同时使用的管理面板和多个员工同时使用的员工面板。管理员可以同时与多个员工聊天,但员工只能与管理员聊天。所有聊天都保存在databasemysql中,并带有各自的from、to、msg和time字段,双方都使用这些字段根据其活动聊天会话匹配消息。 现在我正在使用 setIntervalfunction{$.ajax…;},3000;作为回报,服务器将所有消息返回给管理员,但只返回请求员工的特定消息。但这不是一个实用的解决方案,因为如果它不正确的话,它也会用过多的数据库查询过度使用我的服务器资源。 许多改进是可能的。首先将最后一条消息的时间传递给服务器,然后服务器只发回比该时间晚到达的消息。ajax仍然每3秒调用一次,数据库查询也保持不变。然后我可以使它成为一个具有30秒延迟的自调用函数,如下面的示例所示

waitformsg(){
    $.ajax({ url: "server", success: function(data){
        // Do whatever I wish with the data...
    }, dataType: "json", complete: waitformsg, timeout: 30000 });
}
但即使我使用这种技术,如何在服务器端等待,而不是等待什么? 请记住,我还想减少数据库查询的数量,但我唯一需要的是在发送时间值之后收到的一堆消息。像下面这样的东西可以,但仍然需要等待什么

<?php

    if ( isset($_GET['update']) ){
         $time = $_GET['timestamp'];
         while (...do what here...){
        usleep(10000);
        clearstatcache();
         }
    }
    $response = array();    
    $response['msg'] = "get the message from the database";    
    $response['timestamp'] = time();    
    echo json_encode($response);    
    flush();
}
?>
请帮我解决这个问题。如何设置触发器,在多个用户同时聊天的环境中,我可以在服务器中循环该触发器

我已经实现了轮询技术,到目前为止它看起来不错,但是我仍然不太高兴,因为数据库查询太多了。我仍然愿意听取建议

以下是服务器代码

<?php
    require('database.class.php');
    $database = new database('****','****','****','****');

    if ( isset($_POST['update']) ){
        $lasttime = isset($_POST['timestamp']) ? $_POST['timestamp'] : 0;
        while (1){
            $msgs = $database->executeObjectList("SELECT * FROM tblchatdummy WHERE timestamp > $lasttime");
            if (!empty($msgs)){
                break;
            }
            sleep(2);
            clearstatcache();
        }
        echo json_encode($msgs);
        flush();
    }elseif ( isset($_POST['save']) ){
        $msg = isset($_POST['msg']) ? $_POST['msg'] : '';
        if ($msg != ''){
            $from = $_POST["from"];
            $to = $_POST["to"];
            $timestamp = time();
            $message = filter_var(trim($msg),FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
            $database->executeNonQuery("INSERT INTO tblchatdummy(msg_to, msg_from, msg, timestamp) VALUE('".$to."','".$from."','".$message."','".$timestamp."')");
            $response = array();
            $response['success'] = "1";
            $response['timestamp'] = $timestamp;
            echo json_encode($response);
            flush();
        }
    }
?>
下面是客户端。它简单有效,这就是我喜欢客户端的原因

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  </head>
  <body>
<p>
    <input type="text" name="word" id="word" value="" />
    <input type="button" name="send" value="Send" id="mybutton"/>
</p>
<div id="content"></div>

<script type="text/javascript">
var lastime = 0;
$("#mybutton").click(function(){
    $.post("backend.php", {save:"1",from:"1",to:"5",msg:$("#word").val()}, function(data){
        $("#word").val("");
    }, "json");
});

(function update(){
    $.ajax({ type: "POST", url: "backend.php", data: {update:"1",timestamp:lastime}, success: function(data1){
        lastime = handleDATA(data1);
    }, dataType: "json", complete: update, timeout: 30000 });
})();

function handleDATA (data){
    for(i=0;i<data.length;i++){
        $("#content").append(data[i].msg+"</br>");
    }
    return data[data.length-1].timestamp;
}
</script>

</body>
</html>

再一次,我仍然愿意接受建议。

检查数据库中的数据,如果数据不存在,请等待x秒,然后再次检查,直到y秒过去。y秒过后,不返回任何数据。然后重复该过程。每次通过while循环检查发送一个时间戳,以查看是否已发布新消息。如果不是,就保持循环。如果有一个新的消息退出循环并将结果发送回Ajax,它仍然不会减少数据库查询。我想将查询最小化为仅在触发事件时;时间还不够长,您将要使用大量服务器资源。usleep使用微秒,因此10000微秒=0.01秒。增加到1000000或只使用sleep1;它使用秒数。如果您使用的是时间戳,并且具有正确的索引表,则在服务器上进行DB查询并不困难。