This exact code doesn't work, but, I was hoping something like it was:
这个确切的代码不起作用,但是,我希望它是这样的:
io.sockets.on('connection', function(socket) {
socket.on('heartbeat', function() {
// Do something here...
});
});
Is something like this possible? I mean, I know I can just make a different function that triggers every, say, 15 seconds using a setInterval:
这样的事情可能吗?我的意思是,我知道我可以创建一个不同的函数,使用setInterval触发每个,比如15秒:
io.sockets.on('connection', function(socket) {
setInterval(function() {
// Do something
},15000);
});
But since the heartbeat is already running at this interval, why not make use of it?
但由于心跳已经在这个间隔运行,为什么不利用它呢?
In any case, any insight would be greatly appreciated.
无论如何,我们将非常感谢任何见解。
1 个解决方案
#1
9
I think that I see what you're trying to do. There are a few exposed events that you can check here - list of Socket.io events - but there is no "heartbeat" event that you can tap into to fire at a set interval.
我想我看到你要做的事情。您可以在这里查看一些暴露事件 - Socket.io事件列表 - 但是没有“心跳”事件可以在设定的时间间隔内触发。
You're on the right track with the second piece of code -
你在第二段代码的正确轨道上 -
setInterval(function() {
socket.emit('heartbeat', someData);
}, 5000);
And on the client side -
在客户端 -
socket.on('heartbeat', function(data) {
console.log(data);
})
#1
9
I think that I see what you're trying to do. There are a few exposed events that you can check here - list of Socket.io events - but there is no "heartbeat" event that you can tap into to fire at a set interval.
我想我看到你要做的事情。您可以在这里查看一些暴露事件 - Socket.io事件列表 - 但是没有“心跳”事件可以在设定的时间间隔内触发。
You're on the right track with the second piece of code -
你在第二段代码的正确轨道上 -
setInterval(function() {
socket.emit('heartbeat', someData);
}, 5000);
And on the client side -
在客户端 -
socket.on('heartbeat', function(data) {
console.log(data);
})