Node.js express ws如何定期检查自定义事件并自动执行操作
我使用的是express ws(API,帮助为express和websocket客户端创建服务器) 现在,正如您在上面的代码中看到的,每当创建连接时,它都会检查内容的长度,并在超过0时向客户端发送conetent 以下路由器代码根据web请求更新文件。 如果在连接创建后的某个时候,如果修改了此文件,则此连接不知道该文件,因此未调用send函数,则会出现此问题。 我也试过fs.watch,但我无法让它工作Node.js express ws如何定期检查自定义事件并自动执行操作,node.js,express,websocket,filesystemwatcher,Node.js,Express,Websocket,Filesystemwatcher,我使用的是express ws(API,帮助为express和websocket客户端创建服务器) 现在,正如您在上面的代码中看到的,每当创建连接时,它都会检查内容的长度,并在超过0时向客户端发送conetent 以下路由器代码根据web请求更新文件。 如果在连接创建后的某个时候,如果修改了此文件,则此连接不知道该文件,因此未调用send函数,则会出现此问题。 我也试过fs.watch,但我无法让它工作 router.post('/run_restart', function(req, res,
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();
});
}
});
我想要的是,每当更新文件时,可以为其中一个websocket连接调用ws.send。这段简单的代码适用于express。如果几个延迟对你来说不是问题,你可以使用这个
setInterval(milisecondsToCheck, checkFunction)
更多
如果您这样使用,您可以在完成工作后完成:
var timer = setInterval(milisecondsToCheck, checkFunction);
要清除它:
clearInterval(timer);
由于您的服务器是更改文件的服务器,因此无需使用
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
}
});
请注意:如果您有多个进程或服务器,这将不起作用,但由于您是在向本地文件系统而不是数据库写入数据,因此我认为这不是一个要求。用类似的方法解决了这一问题
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);
现在我假设只有一个客户谢谢你让我调查一下。但问题依然存在。如何为特定连接调用此函数?请检查此选项
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);