通过AJAX监控文件变化,如何?
我正在寻找一种通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要使用 AJAX 监视文件的更改,您可以执行以下操作。
我刚刚测试了它,它运行得很好,但我仍然认为 AJAX 不是正确的选择。对于大文件,比较文件内容会很慢。另外,您提到没有框架,但您应该使用一个用于 AJAX 的框架,只是为了处理跨浏览器的不一致问题。
To monitor a file for changes with AJAX you could do something like this.
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.
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.
我最近从头开始做到了这一点。
我不知道你对 PHP 有多菜鸟(这是我所知道的唯一的服务器脚本语言),但我会尽量简短,如有任何疑问,请随时提出。
我正在使用长轮询,其中包括(
如果你不关心内容文件,只是它已被更改,您可以检查上次修改时间而不是 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 (
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