Javascript 数组数据(在套接字 IO 应用程序中)
首先,这是我的代码:
var users = {};
//User Connect
io.sockets.on('connection', function (socket) {
socket.emit('connected');
socket.on('session', function (session){
users[socket.id] = [session, socket];
console.log(users);
socket.emit("session_established");
//User Disconnect
socket.on('disconnect', function (){
delete users[socket.id];
socket.broadcast.emit('disconnect', { data : session});
});
//The above all works as it should, now here it what I want to do:
socket.on('chatMessage', function (message, userID) {
users[userIDsSocket][userID].emit('chatMessageResponse', { data: message});
});
});
});
这是我的数据的样子(这是针对一个用户的):
{ '17819180631362480757': //this value is users[socket.id]
[ '1', //this value is session at the top, and in the bottom part userID
{ id: '17819180631362480757', //The rest of this is the socket data
namespace: [Object],
manager: [Object],
disconnected: false,
ackPackets: 0,
acks: {},
flags: [Object],
readable: true,
store: [Object],
_events: [Object] } ] }
所以这就是我需要做的事情(就其应如何运作的英语而言):
哦,首先,可以有多个相同的用户ID/会话,如果用户在多个选项卡/计算机/设备中打开窗口,那么它需要将其发送到具有相同用户ID的每个套接字
socket.on('chatMessage', function (message, userID) {
for each (users[*].userID==userID)
{
users[*][userID].emit('chatMessageResponse', { data: message});
}
});
所以我不知道如何我会详细介绍每个部分忽略socket.id(我放置[*]的地方
First of all, here's my code:
var users = {};
//User Connect
io.sockets.on('connection', function (socket) {
socket.emit('connected');
socket.on('session', function (session){
users[socket.id] = [session, socket];
console.log(users);
socket.emit("session_established");
//User Disconnect
socket.on('disconnect', function (){
delete users[socket.id];
socket.broadcast.emit('disconnect', { data : session});
});
//The above all works as it should, now here it what I want to do:
socket.on('chatMessage', function (message, userID) {
users[userIDsSocket][userID].emit('chatMessageResponse', { data: message});
});
});
});
Here's what my data looks like, (and this is for one user):
{ '17819180631362480757': //this value is users[socket.id]
[ '1', //this value is session at the top, and in the bottom part userID
{ id: '17819180631362480757', //The rest of this is the socket data
namespace: [Object],
manager: [Object],
disconnected: false,
ackPackets: 0,
acks: {},
flags: [Object],
readable: true,
store: [Object],
_events: [Object] } ] }
so this is kind of what I need to do (Just in english terms of how it should function):
oh and first, there can be multiple userID's/Session that are the same, if the user has the window open in more than one tab/computer/device, so it needs to send it to every socket that has the same userID
socket.on('chatMessage', function (message, userID) {
for each (users[*].userID==userID)
{
users[*][userID].emit('chatMessageResponse', { data: message});
}
});
So I don't know how I would go through each part disregarding socket.id (where i placed [*]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
改为:
则:
Change to:
then:
我认为您可能需要一个带有
if
条件的for in
循环。I think you might want a
for in
loop, with anif
condition.这是我的代码。
});
here is my code.
});