在socket.io v1中迭代套接字? “......没有方法'客户'' [英] Iterate over sockets in socket.io v1? "...has no method 'clients'"
问题描述
在我写这样的东西之前:
Before I was able to write something like this:
io.sockets.clients().forEach(function (socket) {
socket.emit(signal,data);
});
现在,我无法得到错误 Object#< Namespace>没有方法'客户'
Now, I cannot and I get the error Object #<Namespace> has no method 'clients'
还有其他办法吗?这是使用socket v1.0。 (或者我认为是1.0.2)。
Is there another way to do this? This is with socket v1.0. (or 1.0.2 I think).
为此,我知道我可以使用 io.emit()
,但我想迭代套接字并在计时器中执行它们的功能。我可以将所有内容重构为回调并在 io.on()
上设置计时器,但我想我需要能够使用引用(我认为javascript会复制在这种情况下,对象 socket
而不是引用它?)
For this I know I can use io.emit()
, but I would like to iterate over the sockets and perform functions on them in a timer. I can refactor everything into callbacks and set the timer on io.on()
, but I think I would need to be able to use references (I think javascript would make a copy of the object socket
in this case instead of referencing it?)
这是一个例子
setInterval(function(){
io.sockets.clients().forEach(function (socket) {
socket.emit('newMessage',someCalculations());
});
},1000);
推荐答案
这是我目前的解决方案:
This is my current solution:
var socketList = new Array();
io.on('connection',function(socket){
socketList.push(socket);
});
setInterval(function(){
socketList.forEach(function(){
socket.emit('newMessage',someCalculations());
});
},1000);
这篇关于在socket.io v1中迭代套接字? “......没有方法'客户''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!