如何更新套接字对象的所有客户端在房间? (socket.io) [英] How to update socket object for all clients in room? (socket.io)
本文介绍了如何更新套接字对象的所有客户端在房间? (socket.io)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
io.sockets.on('connection', function(socket) {
socket.object = socket.id;
socket.on('updateObject', function(data) {
// How to update socket.object here for all clients?
});
});
如何做?
推荐答案
请注意,在1.0以上的socket.io版本中,此功能不再可用,建议保留一个socket.id的数组,以便在需要时可以对它们进行迭代是。 由ynos1234示例
Please note that this function is not available anymore in socket.io versions higher then 1.0, it is recommended to keep a array of your socket.id's so you can iterate over them if need be. example by ynos1234
您可以实现这一点与 forEach
函数:
You can achieve this with the forEach
function:
io.sockets.on('connection', function(socket) {
socket.object = socket.id;
socket.on('updateObject', function(data) {
io.sockets.clients('room').forEach(function (socket, data) {
// goes through all clients in room 'room' and lets you update their socket objects
});
});
});
这篇关于如何更新套接字对象的所有客户端在房间? (socket.io)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文