我有一个监视文件更改的应用程序。一旦文件发生变化,就会向所有 socket.io 客户端发出一个事件。这是代码:
io.sockets.on('connection', function(socket) {
fs.watchFile('./file.txt', {persistent:true}, function(data) {
socket.emit('server', {message: 'File changed'});
});
});
Run Code Online (Sandbox Code Playgroud)
我的问题是,上面的代码是否会运行与 socket.io 客户端连接一样多的文件监视进程?
谢谢你。
是的,fs.watchFile()每次客户端连接到服务器时,您的代码都会运行,而您可以尝试。
io.sockets.on('connection',function(socket){
socket.emit('server',{message:'hello'});
});
// the code below will check for change every 100secs and emit a message to all clients of / , and inform that the file has changed
fs.watchFile('FILEPATH', {
persistent: true,
interval: 100000,
},
function(data) {
io.emit('server', { message:'FileChanged' });
},
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2390 次 |
| 最近记录: |