如何每 X 毫秒只执行一次函数?
how to execute a function only once every X milliseconds?
我是 javascript 和节点的新手,目前正在开发 node.js 应用程序,
该应用程序使用 express 和 mongoDB,其想法是通过 webhook、websocket 和 mqtt 监听一些第三方服务,并将所有数据存储到 mongoDB.
但我有一个小问题,一些第三方应用程序经常向我发送数据,
例如,mqtt 流每秒发送大约 2 条消息,我每分钟只需要存储其中一条消息。
这是我将 mqtt 实例化为 app.js
的方式
var mqttHandler = require('./mqtt/mqtt_handler'); //mqtt
var mqttClient = new mqttHandler(); //mqtt
mqttClient.connect(); //mqtt
这是我的 mqttHandler.js:
onst mqtt = require('mqtt');
class MqttHandler {
constructor() {
this.mqttClient = null;
this.host = 'mqtts://host';
this.username = 'foo'; // mqtt credentials if these are needed to connect
this.password = 'mypassqword';
this.port = 8083;
this.protocol = 'MQTTS';
this.client = 'bar'
}
connect() {
// Connect mqtt with credentials (in case of needed, otherwise we can omit 2nd param)
this.mqttClient = mqtt.connect(this.host, {password : this.password, username : this.username, port: this.port});
// Mqtt error calback
this.mqttClient.on('error', (err) => {
console.log(err);
this.mqttClient.end();
});
// Connection callback
this.mqttClient.on('connect', () => {
//console.log(`mqtt client connected`);
});
// mqtt subscriptions
this.mqttClient.subscribe('/the_subscription');
// When a message arrives, console.log it
this.mqttClient.on('message', function (topic, message) {
console.log(message.toString())
});
this.mqttClient.on('close', () => {
//console.log(`mqtt client disconnected`);
});
}
// Sends a mqtt message to topic: mytopic
sendMessage(message) {
this.mqttClient.publish('mytopic', message);
}
}
module.exports = MqttHandler;
我正在阅读有关 setInterval 和 setTimeout 的内容,但我不知道如何实现它们以强制给定函数每 X 秒仅 运行 一次(不知道它被调用了多少次) )
是否有类似/通用的方法来为 mqtt、webohooks 和/或 websocket 实现此功能?
我从教程中获取了这个关于如何实现 mqtt 的示例,它的工作完美,正如我所说,我是 javascript 的新手。
使用 setInterval 的一种天真的方法是定期设置一个标志,并在发布消息后清除它。在间隔函数再次设置标志之前忽略任何其他消息。
let readyToPost = false;
setInterval(function(){ readyToPost = true; }, 1000);
在你的函数中:
function connect() {
if (!readyToPost) return; // do nothing
readyToPost = false;
// rest of your code
}
还有一个模块mqtt的包装器:
const mqttNow = require('mqtt-now');
const options = {
host: 'localhost',
interval: 1000,
actions: [
{
topic: 'public',
message: 'my message'
},
{
topic: 'random',
message: () => ( 'random ' + Math.random() )
}
]
}
mqttNow.publish(options);
我是 javascript 和节点的新手,目前正在开发 node.js 应用程序, 该应用程序使用 express 和 mongoDB,其想法是通过 webhook、websocket 和 mqtt 监听一些第三方服务,并将所有数据存储到 mongoDB.
但我有一个小问题,一些第三方应用程序经常向我发送数据, 例如,mqtt 流每秒发送大约 2 条消息,我每分钟只需要存储其中一条消息。
这是我将 mqtt 实例化为 app.js
的方式var mqttHandler = require('./mqtt/mqtt_handler'); //mqtt
var mqttClient = new mqttHandler(); //mqtt
mqttClient.connect(); //mqtt
这是我的 mqttHandler.js:
onst mqtt = require('mqtt');
class MqttHandler {
constructor() {
this.mqttClient = null;
this.host = 'mqtts://host';
this.username = 'foo'; // mqtt credentials if these are needed to connect
this.password = 'mypassqword';
this.port = 8083;
this.protocol = 'MQTTS';
this.client = 'bar'
}
connect() {
// Connect mqtt with credentials (in case of needed, otherwise we can omit 2nd param)
this.mqttClient = mqtt.connect(this.host, {password : this.password, username : this.username, port: this.port});
// Mqtt error calback
this.mqttClient.on('error', (err) => {
console.log(err);
this.mqttClient.end();
});
// Connection callback
this.mqttClient.on('connect', () => {
//console.log(`mqtt client connected`);
});
// mqtt subscriptions
this.mqttClient.subscribe('/the_subscription');
// When a message arrives, console.log it
this.mqttClient.on('message', function (topic, message) {
console.log(message.toString())
});
this.mqttClient.on('close', () => {
//console.log(`mqtt client disconnected`);
});
}
// Sends a mqtt message to topic: mytopic
sendMessage(message) {
this.mqttClient.publish('mytopic', message);
}
}
module.exports = MqttHandler;
我正在阅读有关 setInterval 和 setTimeout 的内容,但我不知道如何实现它们以强制给定函数每 X 秒仅 运行 一次(不知道它被调用了多少次) )
是否有类似/通用的方法来为 mqtt、webohooks 和/或 websocket 实现此功能?
我从教程中获取了这个关于如何实现 mqtt 的示例,它的工作完美,正如我所说,我是 javascript 的新手。
使用 setInterval 的一种天真的方法是定期设置一个标志,并在发布消息后清除它。在间隔函数再次设置标志之前忽略任何其他消息。
let readyToPost = false;
setInterval(function(){ readyToPost = true; }, 1000);
在你的函数中:
function connect() {
if (!readyToPost) return; // do nothing
readyToPost = false;
// rest of your code
}
还有一个模块mqtt的包装器:
const mqttNow = require('mqtt-now');
const options = {
host: 'localhost',
interval: 1000,
actions: [
{
topic: 'public',
message: 'my message'
},
{
topic: 'random',
message: () => ( 'random ' + Math.random() )
}
]
}
mqttNow.publish(options);