<分区>
我想实时读取一个txt文件并更新我前端添加的新行。该文本文件包含两个用户的日志数据。我已经尝试了一些工作来找到一种工作方式,但是 php 脚本和 js 都没有按我想要的方式工作。
JS 脚本
function ShowNewMessages(res, curr, end) {
// build the front end
var newdiv1 = "<div class=\"message\"> <div class=\"own-avatar-message\" style=\"background-image: url("+res[curr][4].replace(/\\\//g, "/")+");\"></div><div class=\"own-message2\"><div class=\"own-message\"><p>" + res[curr][3] +"</p></div><div class=\"message-timestamp\"> "+ res[curr][1] +" • "+res[curr][2] +"</div></div> </div>";
$( newdiv1 ).insertAfter( $( ".message" ).last());
var height = 0;
$('.message').each(function(i, value){
height += parseInt($(this).height());
});
height += '';
$('div').animate({scrollTop: height});
// check if there is another messsage or checkagain for new messages
curr++;
if (curr <= end) ShowNewMessages(res, curr, end);
else {setTimeout(function() {GetNewMesssages()}, 10000);}
}
function GetNewMesssages() {
$.ajax({
type:'POST',
url: "../lib/GetMesssage.php",
data: "friend=" + MsgFriend,
dataType: 'json',
async: false,
success:function(data){
// check error state
if (data[0] != "Error") {
var start = 0;
var end = data.length-1; // minus one for the new line
if (end > 0 ) ShowNewMessages(data, start, end-1);
else setTimeout(function() {GetNewMesssages()}, 10000);
}
else alert('some error message');
}
});
}
GetMesssage.php
<?php
session_start();
// get the requirements for the file name
$MyEmail = $_SESSION['login'];
$FriendEmail = $_POST['friend'];
if (strcmp($MyEmail, $FriendEmail) < 0) {
$FileName = $MyEmail.'-'.$FriendEmail.'.txt';
}
else {
$FileName = $FriendEmail.'-'.$MyEmail.'.txt';
}
$File = fopen('data/'.$FileName, "r") or die(json_encode(array_push($messages, "Error")));
// count the lines exist in the file
$count = 0;
while(!feof($File)){
$tmp = fgets($File);
$count++;
}
// if the session var that holds the offset position is not set
// or has value more than the lines of text file
if (!isset($_SESSION['pos']) || $_SESSION['pos'] >= $count) {
$_SESSION['pos'] = 0; // back to the beginning
}
// the array that holds the logs data
$messages = array();
// move the pointer to the current position
fseek($File, $_SESSION['pos']);
// read the file
while(!feof($File)){
$msg = fgets($File);
$msg1 = explode("<!@#@>", $msg);
array_push($messages, $msg1);
$_SESSION['pos']++;
}
// get the current offset position
//$_SESSION['pos'] = ftell($File);
// return the array
echo json_encode($messages);
fclose($File)
?>
解释
假设当我们调用 GetNewMesssages()
函数时例程第一次启动。该函数将触发 php 并从文件中获取数据。
接下来,如果数据有内容,返回数组不为空,我们执行 ShowNewMessages()
函数并更新前端。
完成后,我们开始另一个 GetNewMesssages()
回调。
我想要什么
我想每 N 秒检查一次文件中是否有新添加的行并更新我的前端。
问题
我在 php 和 js 脚本中都遇到了问题。我不想添加两个问题。
在 PHP 中
我没有找到如何使用 ftell()
来只获取新添加的行。由于脚本按原样运行,每次更新时我都会不断地将所有文件放入我的数组中。
在 JS 中
在 JS 中,我在同步 ajax 调用时遇到了问题。这意味着在每次调用 GetNewMessages()
函数时,我都会再次调用该函数 2-3 次。它与 setTimeout()
有关,但我也找不到如何设置回调。
更新
我读取的文本文件示例:
line_id <BreakString> some data <BreakString> blah blah
another_line_id <BreakString> some_other data <BreakString> blah blah 2
注意:没有任何意义。它只是将内容分开到数组。
更新 2
在版主通知后,我会缩小我的问题,只是为了找到实时读取文件以保持我的结构的最佳方法。