Javascript 数组数据(在套接字 IO 应用程序中)

发布于 2025-01-04 08:50:51 字数 1535 浏览 0 评论 0原文

首先,这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小矜持 2025-01-11 08:50:51

改为:

users[socket.id] = {userID:session, socketID:socket};

则:

for (var key in users)
{
if (users[key].userID==userID)
{

users[key].socketID.emit('new_request_response', { data: message});
}
}

Change to:

users[socket.id] = {userID:session, socketID:socket};

then:

for (var key in users)
{
if (users[key].userID==userID)
{

users[key].socketID.emit('new_request_response', { data: message});
}
}
余罪 2025-01-11 08:50:51

我认为您可能需要一个带有 if 条件的 for in 循环。

socket.on('chatMessage', function (message, userID) {
    for (var key in users)
        if (users[key].userId==userId)
            users[key][userId].emit('chatMessageResponse', { data: message});
});

I think you might want a for in loop, with an if condition.

socket.on('chatMessage', function (message, userID) {
    for (var key in users)
        if (users[key].userId==userId)
            users[key][userId].emit('chatMessageResponse', { data: message});
});
强辩 2025-01-11 08:50:51

这是我的代码。

var io = require('socket.io').listen(15630);
var clients = [];
var coupling = [];

io.sockets.on('connection', function (socket) {

clients[socket.id] = socket;
socket.on('setUserID', function (userID) {

    socket.set('userID', userID, function () {
        coupling[userID] = socket.id;
    });

});


for (var client in io.sockets.clients()) {

        console.log(client);

}

socket.on('send', function (from,to,message)
{

    var sender = 0;
    socket.get('userID', function (err, name) {
        sender = name;
    });
    var userSocket = clients[coupling[to]];
    if(userSocket != null || userSocket != undefined)
    userSocket.emit('message', {from :from,to : to,message:message});
});
socket.on('disconnect', function () {

    var sender = 0;
    socket.get('userID', function (err, name) {
        sender = name;
    });
    delete clients[coupling[sender]]
    //var userSocket = clients[coupling[sender]];
    //if(userSocket != null || userSocket != undefined)
    //clients[coupling[sender]] = null;
});

});

here is my code.

var io = require('socket.io').listen(15630);
var clients = [];
var coupling = [];

io.sockets.on('connection', function (socket) {

clients[socket.id] = socket;
socket.on('setUserID', function (userID) {

    socket.set('userID', userID, function () {
        coupling[userID] = socket.id;
    });

});


for (var client in io.sockets.clients()) {

        console.log(client);

}

socket.on('send', function (from,to,message)
{

    var sender = 0;
    socket.get('userID', function (err, name) {
        sender = name;
    });
    var userSocket = clients[coupling[to]];
    if(userSocket != null || userSocket != undefined)
    userSocket.emit('message', {from :from,to : to,message:message});
});
socket.on('disconnect', function () {

    var sender = 0;
    socket.get('userID', function (err, name) {
        sender = name;
    });
    delete clients[coupling[sender]]
    //var userSocket = clients[coupling[sender]];
    //if(userSocket != null || userSocket != undefined)
    //clients[coupling[sender]] = null;
});

});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文