如何打破 io 套接字中的 setinterval - Nodejs
How to break setinterval in io sockets - Nodejs
有一个 api 发送一些 json data.The nodejs 服务器获取此 json 数据并每 5 seconds.If 连接发送带有 websocket 的客户端当客户端连接时打开它工作但是当客户端断开连接时它不会停止。
代码
io.on('connection', function(client) {
var loop=setInterval(()=>{
console.log('Client connected...');
fetch('https://www.foo.com/api/v2/searchAssets')
.then(res => res.json())
.then(json =>
{client.emit('news'{json});console.log(json)}),5000);
})});
io.on('disconnetion',function(){
clearInterval(loop);
console.log("disconnected");
})
或
除了 websocket 之外,您还有其他建议可以将此 json 数据发送到客户端吗?
在此先感谢您的支持
你的问题是范围问题。当您声明 loop
var 时,它是 on connection
事件回调的局部变量,不存在于 on disconnect
事件中。根据如何 handle disconnection 的文档,您可以像这样在连接处理程序中移动断开连接处理程序:
io.on('connection', function(client) {
// Start the interval
var loop = setInterval(()=>{
console.log('Client connected...');
fetch('https://www.foo.com/api/v2/searchAssets')
.then(res => res.json())
.then(json => {
client.emit('news'{json});console.log(json)
} ,5000);
});
// Handles disconnection inside the on connection event
// Note this is using `client.on`, not `io.on`, and that
// your original code was missing the "c" in "disconnect"
client.on('disconnect', () => {
clearInterval(loop);
console.log("disconnected");
});
});
但我不推荐这种架构,因为流式数据独立于客户端。数据可以一次获取并流式传输给所有人。方法如下:
var loop
// The function startStreaming starts streaming data to all the users
function startStreaming() {
loop = setInterval(() => {
fetch('https://www.foo.com/api/v2/searchAssets')
.then(res => res.json())
.then(json => {
// The emit function of io is used to broadcast a message to
// all the connected users
io.emit('news', {json});
console.log(json);
} ,5000);
});
}
// The function stopStreaming stops streaming data to all the users
function stopStreaming() {
clearInterval(loop);
}
io.on('connection',function() {
console.log("Client connected");
// On connection we check if this is the first client to connect
// If it is, the interval is started
if (io.sockets.clients().length === 1) {
startStreaming();
}
});
io.on('disconnetion',function() {
console.log("disconnected");
// On disconnection we check the number of connected users
// If there is none, the interval is stopped
if (io.sockets.clients().length === 0) {
stopStreaming();
}
});
有一个 api 发送一些 json data.The nodejs 服务器获取此 json 数据并每 5 seconds.If 连接发送带有 websocket 的客户端当客户端连接时打开它工作但是当客户端断开连接时它不会停止。
代码
io.on('connection', function(client) {
var loop=setInterval(()=>{
console.log('Client connected...');
fetch('https://www.foo.com/api/v2/searchAssets')
.then(res => res.json())
.then(json =>
{client.emit('news'{json});console.log(json)}),5000);
})});
io.on('disconnetion',function(){
clearInterval(loop);
console.log("disconnected");
})
或
除了 websocket 之外,您还有其他建议可以将此 json 数据发送到客户端吗?
在此先感谢您的支持
你的问题是范围问题。当您声明 loop
var 时,它是 on connection
事件回调的局部变量,不存在于 on disconnect
事件中。根据如何 handle disconnection 的文档,您可以像这样在连接处理程序中移动断开连接处理程序:
io.on('connection', function(client) {
// Start the interval
var loop = setInterval(()=>{
console.log('Client connected...');
fetch('https://www.foo.com/api/v2/searchAssets')
.then(res => res.json())
.then(json => {
client.emit('news'{json});console.log(json)
} ,5000);
});
// Handles disconnection inside the on connection event
// Note this is using `client.on`, not `io.on`, and that
// your original code was missing the "c" in "disconnect"
client.on('disconnect', () => {
clearInterval(loop);
console.log("disconnected");
});
});
但我不推荐这种架构,因为流式数据独立于客户端。数据可以一次获取并流式传输给所有人。方法如下:
var loop
// The function startStreaming starts streaming data to all the users
function startStreaming() {
loop = setInterval(() => {
fetch('https://www.foo.com/api/v2/searchAssets')
.then(res => res.json())
.then(json => {
// The emit function of io is used to broadcast a message to
// all the connected users
io.emit('news', {json});
console.log(json);
} ,5000);
});
}
// The function stopStreaming stops streaming data to all the users
function stopStreaming() {
clearInterval(loop);
}
io.on('connection',function() {
console.log("Client connected");
// On connection we check if this is the first client to connect
// If it is, the interval is started
if (io.sockets.clients().length === 1) {
startStreaming();
}
});
io.on('disconnetion',function() {
console.log("disconnected");
// On disconnection we check the number of connected users
// If there is none, the interval is stopped
if (io.sockets.clients().length === 0) {
stopStreaming();
}
});