大佬教程收集整理的这篇文章主要介绍了node.js – 表示如何定期检查自定义事件并自动执行操作,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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',flags) { var data = []; // List of Buffer objects res.on("data",function(chunk) { data.push(chunk); // Append Buffer object console.log(data) }) }) });
现在您可以看到上面的代码,无论何时创建连接,它都会检查内容的长度,并在超过0时向客户端发送conetent.
遵循路由器代码,在Web请求上更新文件.
如果在连接创建后的某个时间(如果此文件被修改),此连接不知道它,因此不会调用此函数,因此不会调用send函数.
我也试过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',data) { if (err) throw err; content = data.toString(); }); } });
var connections = []; // Keeps track of all connections app.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(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',{ title: 'Stressem' }); connections.forEach(function(C){ c.send(text); // Send the new text to all open connections } });
以上是大佬教程为你收集整理的node.js – 表示如何定期检查自定义事件并自动执行操作全部内容,希望文章能够帮你解决node.js – 表示如何定期检查自定义事件并自动执行操作所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。