为什么我的io.on被连续触发?

问题描述 投票:0回答:1

我一直在尝试在项目中使用socket.io,但是我的io.on不断被触发。我不明白为什么它在循环中。正如其他答案所建议的那样,我已经对版本进行了交叉检查,完全相同。任何帮助都会很棒。这是它的代码服务器:-

io.on('connection', async function(socket){
       //These both lines are being called again and again
        console.log("Test"); 
        socket.emit('new_token');
    });

这将是一个有角度的消费者:

 this.socket.on('new_token', () => {
     // console.log(data);
        this.notifier.notify('warning','Embeded token has been updated. Reloading in 3 seconds. !');
        setTimeout(()=>{
          window.location.reload();
        }, 3000);

    });
node.js angular socket.io
1个回答
1
投票

每次加载或重新加载网页时,都会创建一个新的套接字连接。您在每个连接上都发出“ new_token”,然后侦听器通过window.location.reload()重新加载页面,这将再次触发连接事件,依此类推。因此,这就像无限循环一样,只是您来回移动。

© www.soinside.com 2019 - 2024. All rights reserved.