expression – 如何定期检查自定义事件并自动采取行动

我正在使用express-ws https://www.npmjs.com/package/express-ws(API可帮助为express和websocket客户端创build服务器)。

app.ws('/', function(ws, req) { console.log("New connection") if (content.length > 0) { console.log(content) ws.send(content) } ws.on('message', function(msg, flags) { console.log("Received "+ msg); }); ws.on('data', function(msg, flags) { var data = []; // List of Buffer objects res.on("data", function(chunk) { data.push(chunk); // Append Buffer object console.log(data) }) }) }); 

现在,您可以看到上面的代码,每当创build一个连接时,它会检查内容的长度,如果超过0,则将内容发送给客户端。

在路由器代码之后,在Web请求中,更新文件。 如果这个文件被修改了,连接创build后有问题,这个连接不知道它,因此发送函数不被调用。 我也尝试fs.watch,但我无法使其工作。

 router.post('/run_restart', function(req, res, next) { text = '{"to_do": "run_test", "devices":"all", "argv": { "test": "' + req.body.cmd + '", "cycles": "' + req.body.cycles + '", "awake_for": "' + req.body.wt + '" }}' path = process.env['HOME']+'/Desktop/automation/Stressem/StressemWeb/bin/task.txt' fs.writeFile(path, text) res.render('home.jade', { title: 'Stressem' }); }); fs.watch(file, function (event) { fs.stat(file, function (err, stats) { if(stats.size>80){ console.log("Event: " + event); fs.readFile(file, 'utf8', function (err, data) { if (err) throw err; content = data.toString(); }); } }); 

我想要的是每当文件更新,ws.send可以被调用的一个websocket连接。

由于您的服务器是更改文件的服务器,因此您无需像使用fs.watch那样知道文件更改的时间。 剩下要做的就是迭代一个打开的连接列表并发送新的内容。

 var connections = []; // Keeps track of all connections app.ws('/', function(ws, req) { console.log("New connection") connections.push(ws); // Add the new connection to the list if (content.length > 0) { console.log(content) ws.send(content) } ws.on('message', function(msg, flags) { console.log("Received "+ msg); }); ws.on('data', function(msg, flags) { var data = []; // List of Buffer objects res.on("data", function(chunk) { data.push(chunk); // Append Buffer object console.log(data) }) }) // TODO: Make sure you remove closed connections from `connections` // by listening for the ws `close` event. }); router.post('/run_restart', function(req, res, next) { text = '{"to_do": "run_test", "devices":"all", "argv": { "test": "' + req.body.cmd + '", "cycles": "' + req.body.cycles + '", "awake_for": "' + req.body.wt + '" }}' path = process.env['HOME']+'/Desktop/automation/Stressem/StressemWeb/bin/task.txt' fs.writeFile(path, text) res.render('home.jade', { title: 'Stressem' }); connections.forEach(function(c){ c.send(text); // Send the new text to all open connections } }); 

请注意:如果您有多个进程或服务器,这将不起作用,但由于您正在写入本地文件系统而不是数据库,因此我认为这不是必需的。

这个简单的代码与快递工作良好。 如果有一些延迟对你没有问题,你可以使用这个。

 setInterval(milisecondsToCheck, checkFunction) 

为更多

http://www.w3schools.com/jsref/met_win_setinterval.asp

https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval

如果你这样使用它,你可以在完成工作后完成它:

 var timer = setInterval(milisecondsToCheck, checkFunction); 

要清除它:

 clearInterval(timer); 

用这样的东西解决这个问题

 var conn_array = []; app.ws('/', function(ws, req) { conn_array.push(ws) console.log("New connection") fs.readFile(file, 'utf8', function (err, data) { if (err) throw err; content = data.toString(); if (content.length > 0) { console.log(content.length) conn_array[0].send(content) } }); ws.on('message', function(msg, flags) { console.log("Received "+ msg); }); ws.on('data', function(msg, flags) { var data = []; // List of Buffer objects res.on("data", function(chunk) { data.push(chunk); // Append Buffer object console.log(data) }) }) }); function readFile(){ console.log("I am here") fs.readFile(file, 'utf8', function (err, data) { if (err) throw err; content = data.toString(); if (content.length > 0 && conn_array.length>0) conn_array[0].send(content); }) } var interval = setInterval(readFile, 100000); 

现在我假设只有一个客户