【问题标题】:Why my io.on is being fired continuously?为什么我的 io.on 不断被解雇?
【发布时间】:2019-11-13 12:53:01
【问题描述】:

我一直在尝试在项目中使用 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】:

    每次网页加载或重新加载时,都会创建一个新的套接字连接。您在每个连接上发出“new_token”,然后您的侦听器正在通过 window.location.reload() 重新加载页面,这将再次触发连接事件等等。 所以这是一种无限循环,因为你只是来回走动。

    【讨论】:

      猜你喜欢
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-11
      • 2011-11-28
      相关资源
      最近更新 更多