通过AJAX监控文件变化,如何?

发布于 2024-11-28 21:14:56 字数 238 浏览 1 评论 0原文

我正在寻找一种通过 AJAX(而不是通过 JS 框架!)来实时监视文件更改的方法。如果对该文件进行更改,我需要它发出警报消息。我是一个 AJAX 菜鸟,所以请保持温柔。 ;-)

编辑:让我更详细地解释一下目的。我正在使用我用 PHP 为 webhop 编写的聊天脚本,我想要的是从管理模块监视聊天请求。聊天内容存储在文本文件中,如果有人开始聊天会话,则会创建一个新文件。如果是这样的话,我想在管理模块中实时看到这一点。

有道理吗?

I'm looking for a way through AJAX (not via a JS framework!) to real time monitor a file for changes. If changes where made to that file, I need it to give an alert message. I'm a total AJAX noob, so please be gentle. ;-)

Edit: let me explain the purpose a bit more in detail. I'm using a chat script I've written in PHP for a webhop, and what I want is from an admin module monitor the chat requests. The chats are stored in text files, and if someone starts a chat session a new file is created. If that's the case, in the admin module I want to see that in real time.

Makes sense?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别念他 2024-12-05 21:14:56

要使用 AJAX 监视文件的更改,您可以执行以下操作。

var previous = "";

setInterval(function() {
    var ajax = new XMLHttpRequest();
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            if (ajax.responseText != previous) {
                alert("file changed!");
                previous = ajax.responseText;
            }
        }
    };
    ajax.open("POST", "foo.txt", true); //Use POST to avoid caching
    ajax.send();
}, 1000);

我刚刚测试了它,它运行得很好,但我仍然认为 AJAX 不是正确的选择。对于大文件,比较文件内容会很慢。另外,您提到没有框架,但您应该使用一个用于 AJAX 的框架,只是为了处理跨浏览器的不一致问题。

To monitor a file for changes with AJAX you could do something like this.

var previous = "";

setInterval(function() {
    var ajax = new XMLHttpRequest();
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            if (ajax.responseText != previous) {
                alert("file changed!");
                previous = ajax.responseText;
            }
        }
    };
    ajax.open("POST", "foo.txt", true); //Use POST to avoid caching
    ajax.send();
}, 1000);

I just tested it, and it works pretty well, but I still maintain that AJAX is not the way to go here. Comparing file contents will be slow for big files. Also, you mentionned no framework, but you should use one for AJAX, just to handle the cross-browser inconsistencies.

抚笙 2024-12-05 21:14:56

AJAX 只是一个 JavaScript,因此从其定义来看,您没有任何工具可以访问文件,除非其他服务调用 js/AJAX 来通知更改。

AJAX is just a javascript, so from its definition you do not have any tool to get access to file unless other service calls an js/AJAX to notify about the change.

掐死时间 2024-12-05 21:14:56

我最近从头开始做到了这一点。

我不知道你对 PHP 有多菜鸟(这是我所知道的唯一的服务器脚本语言),但我会尽量简短,如有任何疑问,请随时提出。

我正在使用长轮询,其中包括(

  1. 创建一个 PHP 脚本,定期检查文件的内容,并且仅在看到任何更改时才响应(它可以在响应中包含更改的描述)
  2. 创建 XHR 对象
  3. 包括您的通知代码作为回调函数(它可以使用描述)
  4. 发出请求
  5. PHP 脚本将开始检查文件,但在发生更改之前不会回复
  6. 当它响应时,将调用回调并且您的通知代码将launch

如果你不关心内容文件,只是它已被更改,您可以检查上次修改时间而不是 PHP 脚本中的内容

编辑:从一些评论中我看到有一些东西可以监视称为 FAM 的文件更改,这似乎是要走的路。对于 PHP 脚本

I've done that from scratch recently.

I don't know how much of a noob you are with PHP (it's the only server script language I know), but I'll try to be as brief as possible, feel free to ask any doubt.

I'm using long polling, which consists in this (

  1. Create a PHP script that checks the content of the file periodically and only responds when it sees any change (it could include a description of the change in the response)
  2. Create your XHR object
  3. Include your notification code as a callback function (it can use the description)
  4. Make the request
  5. The PHP script will start checking the file, but won't reply until there is a change
  6. When it responds, the callback will be called and your notification code will launch

If you don't care about the content of the file, only that it has been changed, you can check the last-modified time instead of the content in the PHP script.

EDIT: from some comment I see there's something to monitor file changes called FAM, that seems to be the way to go for the PHP script

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文