在随后的请求(使用快递)中,我试图通过节点进程使用setTimeout方法清除超时集。因此,基本上,当我们的实时流事件开始时,我设置了超时(由web钩子通知),并打算在一小时后停止对来宾用户的访问。一个小时是通过setTimeout计算的,到目前为止,这是很好的。但是,如果事件在一小时前停止,我需要清除超时。我正在尝试使用clearTimeOut,但它只是找不到相同的变量。
// Event starts
var setTimeoutIds = {};
var val = req.body.eventId;
setTimeoutIds[val] = setTimeout(function() {
req.app.io.emit('disable_for_guest',req.body);
live_events.update({event_id:req.body.eventId},{guest_visibility:false},function(err,data){
//All ok
});
}, disable_after_milliseconds);
console.log(setTimeoutIds);
req.app.io.emit('session_started',req.body);
When event ends:
try{
var event_id = req.body.eventId;
clearTimeout(setTimeoutIds[event_id]);
delete setTimeoutIds[event_id];
}catch(e){
console.log('Event ID could not be removed' + e);
}
req.app.io.emit('event_ended',req.body);产出:
发布于 2018-07-16 09:27:04
在处理程序的作用域中定义setTimeoutIds。您必须在模块级别定义它。
var setTimeoutIds = {};
router.post('/webhook', function(req, res) {
...这使得变量在服务器下一次重新启动之前可用。
注:这种方法只有在只有一个服务器和一个节点进程为您的应用程序服务时才能工作。一旦您进入多进程和/或多服务器,您需要一种完全不同的方法。
https://stackoverflow.com/questions/51301332
复制相似问题