首页 > 解决方案 > php xdiff_file_diff 和 xdiff_file_patch 函数用于加载新的聊天消息

问题描述

这是一个聊天脚本:

  1. 使用操作 process.php 在表单提交时附加 newfile.txt 中的键入文本。
  2. 如果 newfile.txt 和 oldfile.txt 之间存在差异,那么我使用 setInterval 使用 ajax 检查与 xdiff_file_diff() 的差异,并将差异写入 diff.txt。
  3. 该脚本使用 xdiff_file_patch() 修补 oldfile.txt 以消除差异。

该脚本的目的是使用 jQuery 将 .append() 仅将新消息添加到聊天 div 中以获取消息,因此整个聊天消息不会重新加载,因为我在 iframe 中有 youtube 视频,所以它们不会被打断,总的来说,我认为这是更好的方法。

我有一个问题,代码中的逻辑是否正常,因为我看到在聊天中打印出旧的输入,我将如何去除 diff.txt 中的混乱并只显示文本差异?

我拥有的文件:

process.php 文件的一部分

$myfile = fopen('newfile.txt', 'a') or die('unable to open file');
$txt = $_POST['text'] . "\n";
fwrite($myfile, $_SESSION['username'] . ': ' . $txt);

加载消息.php

xdiff_file_diff('oldfile.txt', 'newfile.txt', 'diff.txt');
$myfile = fopen('diff.txt', 'r') or die('unable to open file');
if(filesize('diff.txt') > 0)
{
    $text = fread($myfile, filesize('diff.txt'));
    $text = str_replace('\n', '<br />', $text);

    $yt = linkifyYouTubeURLs($text);
    echo '<div class="message">' . filterText($array, $yt) . '</div>';


    xdiff_file_patch('newfile.txt', 'diff.txt', 'oldfile.txt');
    file_put_contents('diff.txt', '');


}
fclose($myfile);

阿贾克斯

function loadmessages2()
        {
            $.ajax({
                method: 'post',
                url: 'loadmessages.php',
                success: function(data){
                    if(data != '')
                    {
                        $('#chat_bubble').append(data);

                        $(document).on('submit','#frmsend',function(){
                            $("#chat_bubble").stop().animate({scrollTop: $("#chat_bubble")[0].scrollHeight}, 1000);


                        });


                        $("#chat_bubble").stop().animate({scrollTop: $("#chat_bubble")[0].scrollHeight}, 1000);
                    }
                }
            });
        }

结果演示是:

在此处输入图像描述

标签: php

解决方案


推荐阅读