10162 后台优化(优化聊天监控:支持长轮询方式,聊天内容不删除,重复打开依然显示最近x条;)
1个文件已修改
1个文件已添加
131 ■■■■■ 已修改文件
serverinfo/chatmonitor.php 49 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
serverinfo/chatmonitors2.php 82 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
serverinfo/chatmonitor.php
@@ -92,6 +92,9 @@
<script type='text/javascript' src="/js/common.js"></script>
<script type="text/javascript">
    var linkType = 1; // 连接方式: 0-默认使用socket;1-长轮询
    var lastChat = {};
    var errorCount = 0;
    var address = "<?php echo $address ?>";
    var port = "<?php echo $port ?>";
    var spid = "<?php echo $spid ?>";
@@ -107,8 +110,51 @@
    var scrolling = 1; //自动滚屏,默认开启
    window.onload = function() {
        createWebSocket();
        if (linkType == 0) {
            createWebSocket();
        } else {
            requestChatContent();
        }
    }
    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }
    function requestChatContent() {
        // console.log("requestChatContent", spid, lastChat);
        var params = "spid=" + spid;
        if (lastChat.AccountID) {
            params += "&AccountID=" + lastChat.AccountID + "&Time=" + lastChat.Time + "&Content=" + lastChat.Content;
        }
        ajaxRequest("chatmonitors2.php?" + params,
            function(xmlhttp) {
                var sleepSeconds = 1;
                if (xmlhttp.responseText) {
                    try {
                        var parseData = JSON.parse(xmlhttp.responseText);
                        // console.log(parseData);
                        switch (parseData.MsgType) {
                            case "CHAT":
                                addChatContent(parseData.Msg);
                                break;
                            default:
                                break;
                        }
                    } catch (error) {
                        errorCount += 1;
                        console.error("error:", errorCount, error);
                        // 每错一次多暂停1秒,最高暂停1分钟
                        sleepSeconds = Math.min(errorCount, 60);
                    }
                }
                sleep(sleepSeconds * 1000).then(() => {
                    requestChatContent();
                });
            }, "POST"
        );
    }
    // 更新敏感词
    function updSensitiveWord(words) {
@@ -154,6 +200,7 @@
            if (chat.OperatorID != spid) {
                continue;
            }
            lastChat = chat;
            // 替换敏感词样式
            let content = chat.Content;
            for (let i = 0; i < SensitiveWords.length; i++) {
serverinfo/chatmonitors2.php
New file
@@ -0,0 +1,82 @@
<?php
include_once "/Common/Logging.php";
include_once "/Common/CommFunc.php";
include_once "/Account/User.php";
include_once '/db/RedisOper.php';
\Logging\CreateLogging("chatmonitorserver2.php");
\Logging\LogInfo("_POST:" . print_r($_POST, true));
$spid = $_POST["spid"];
$interfaceConfig = parse_ini_file("/InterfaceConfig.php", true);
$GameName = $interfaceConfig["ServerInfo"]["GameName"];
$CacheChatCount = $interfaceConfig["Chatmonitor"]["CacheChatCount"];
$redisKey = \RedisOper\GetFCRedisKey($GameName, "Chatmonitor", $spid);
$retArray = array();
if ($_POST["AccountID"]) {
    $chat["Time"];
    $doCountMax = 12; // 执行取数据次数
    $perCountSleep = 5; // 每次执行后休眠x秒
    $sleepTime = $doCountMax * $perCountSleep;
    if ($sleepTime >= 30) {
        set_time_limit($sleepTime + 30); // 限制执行时长秒 30秒 + 休眠时长
    }
    // 直到取到有新数据
    $doCount = 0;
    while ($doCount < $doCountMax && count($retArray) <= 0) {
        $doCount += 1;
        if ($doCount > 1) {
            sleep($perCountSleep);
        }
        // 取最后一条之后的数据
        $startIndex = 0;
        $stopIndex = -1;
        $perGetCount = 10; // 每次一次性取几条数据
        $loopCount = $CacheChatCount / $perGetCount;
        // \Logging\LogInfo("doCount:" . $doCount . " CacheChatCount :" . $CacheChatCount . " loopCount:" . $loopCount);
        $isEnd = false;
        while (!$isEnd && $loopCount >= 0) {
            $loopCount -= 1;
            $startIndex -= $perGetCount;
            // \Logging\LogInfo("i :" . $i . " index:" . $startIndex . " ~ " . $stopIndex);
            // 从后往前读
            if (!\RedisOper\ListRange($redisKey, $ret, $startIndex, $stopIndex)) {
                break;
            }
            if (isset($ret) && count($ret) > 0) {
                // \Logging\LogInfo("retCount:" . count($ret));
                // 倒序遍历
                for ($j = count($ret) - 1; $j >= 0; $j--) {
                    $chat = $ret[$j];
                    if ($chat["Time"] < $_POST["Time"] && $_POST["Time"] && $_POST["Time"] != "undefined") {
                        $isEnd = true;
                        break;
                    }
                    if ($chat["AccountID"] == $_POST['AccountID'] && $chat["Time"] == $_POST['Time'] && $chat["Content"] == $_POST['Content']) {
                        $isEnd = true;
                        break;
                    }
                    array_splice($retArray, 0, 0, array($chat));
                    // \Logging\LogInfo("array_splice count:" . count($retArray) . print_r($chat, true));
                }
            } else {
                $isEnd = true;
            }
            $stopIndex = $startIndex - 1;
        }
    }
} else {
    // 全部
    if (!\RedisOper\ListRange($redisKey, $retArray, 0, -1)) {
        exit;
    }
}
if (!isset($retArray) || count($retArray) <= 0) {
    exit;
}
$msg = array("MsgType" => "CHAT", "Msg" => $retArray);
echo json_encode($msg);