0

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 [*]

3 Answers 3

1

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});
}
}
Sign up to request clarification or add additional context in comments.

Comments

0

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});
});

2 Comments

Thanks but I can't get it to work, I can get it to do this and send it to everyone that is connected, however there is no userID listed, so that's not calling anything
awesome. The one thing you want to look out for when using for in is that you might get it looping on a property you don't expect, like a prototype method or something, so it might be safest to add another if condition, like if (object.hasOwnProperty(key)) Good luck, and happy coding!
0

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;
});

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.