php - php xdiff_file_diff 和 xdiff_file_patch 函数用于加载新的聊天消息
问题描述
这是一个聊天脚本:
- 使用操作 process.php 在表单提交时附加 newfile.txt 中的键入文本。
- 如果 newfile.txt 和 oldfile.txt 之间存在差异,那么我使用 setInterval 使用 ajax 检查与 xdiff_file_diff() 的差异,并将差异写入 diff.txt。
- 该脚本使用 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);
}
}
});
}
结果演示是:
解决方案
推荐阅读
- python - 在python中对嵌套字典进行排序
- javascript - jQuery 从 $(this) 切换下一个元素
- c# - 如何在运行时添加视图而不使用 wpf 中的 ViewModel
- python - 如何使用张量流在推理阶段加载移动平均权重变量
- django - django PasswordChangeView 总是重定向到登录
- android - 反应原生android错误SyntaxError:JSON中位置0的意外标记
- r - 无法打开共享对象文件:没有这样的文件或目录 Rglpk
- asp.net - 如何将数据发送到客户端系统的端口?
- sql - 执行批处理时出错。错误消息是:指定的签名大小为零。(HRESULT 异常:0x80131237)
- java - 为什么在下面的代码中使用两个大括号?这里不是匿名内部类)