Change user status when online with websockets (php Ratchet)

Asked

Viewed 161 times

0

I’m making a chat using websockets with Ratchet, and I have a question regarding the user status.

I made the page upload a list of all the contacts that the user has, and when the server receives a connection from a user I do a search in this contact list by the user who just signed in doing the following:

Onmessage event:

conn.onmessage = function (event) {
    //console.log(event);

    var package = JSON.parse(event.data);

    if (package.type == 'message') {
    dialog_output(package);
    } else if (package.type == 'userlist') {
    users_output(package.users);
    }
};

Function that changes status color:

    function users_output(users) {

    for (var i in users) {

    if (users.hasOwnProperty(i)) {
        var user = users[i];

        user_list.find('li').each(function(i){
            if($(this).attr("data-id") == user.id){
                $(this).css("color", "rgb(50, 250, 0)");
            }else{
                $(this).css("color", "#000");
            }
        });

    }
    }
}

It’s working that way, but I was wondering if it’s the right thing to do? Because it occurred to me that regardless of whether the new user who just logged in is or is not logged in, he will still compare it to the contact list.

  • 1

    You can add a trigger to mark users online through id. if (package.type == 'useronline') {
 setUserOnline(package.user.id);
}. This way you could create a function to capture the li[id="ID-DO-USUARIO"] and color his name.

  • But how do you do it? If you can assemble a complete answer so I can mark it as correct. I’m new to this area of websockets, and I imagine it’s not enough to just put an if there on onmessage @Valdeirpsr

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.