node.js - 为什么我的 io.on 不断被解雇?
问题描述
我一直在尝试在项目中使用 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);
});
解决方案
每次网页加载或重新加载时,都会创建一个新的套接字连接。您在每个连接上发出“new_token”,然后您的侦听器通过 window.location.reload() 重新加载页面,这将再次触发连接事件等等。所以这是一种无限循环,因为你只是来回走动。
推荐阅读
- reactjs - 将附加参数传递给 React Ant Design Select 组件的 onSelect 方法
- jsf - 如何在往返外部站点后保持 p:tabView 选项卡处于活动状态
- swift - 如何在 Swift 中修复 UIToolbar 和 UIBarButtonItem 之间的空白(初学者)
- android - 如何在 GitHub Actions 中选择构建变体(Android)?
- javascript - 在javascript中反转字符串的最快方法是什么?
- r - DescTools 中曲线下的区域忽略了我的限制
- django - KeyError:尝试使用update_or_create()方法时django rest框架中的'id'
- asynchronous - 如何创建 Tokio 计时器来消除网络数据包的接收抖动?
- pytorch - 在 PyTorch 中训练后模型未保存
- protocol-buffers - 序列化对象/消息作为 protobuf 中的类型