一个文件监视所有的socket.io连接

我有一个应用程序,看一个文件的变化。 一旦文件改变了,一个事件就会被发送到所有的socket.io客户端。 这里是代码:

io.sockets.on('connection', function(socket) { fs.watchFile('./file.txt', {persistent:true}, function(data) { socket.emit('server', {message: 'File changed'}); }); }); 

我的问题是,上面的代码是否真的会运行与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('FILE',{persistent:true,interval:100000,function(data){ io.emit('server',{message:'FileChanged'}; }