Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
821 views
in Technique[技术] by (71.8m points)

node.js - All messages appear on the WebSocket channel

I am aiming to write a custom two-sided message system using socket.io and node.js. Now I made a system that checks the online status of the counter user. Each user has a unique token. In the code below, if the current user is in a room, that is, in the area where he will be messaging with the other user. To get the user's online status, he first gets his token. Then it sends it to the server with its own token.

    setInterval( function() {
        var other_user_token = bp_user_info.attr("data-user-token");
        if ( other_user_token ) {
            socket.emit('online_status',{
                user_token: bp_messenger.user_token,
                other_user_token: other_user_token
            });
        }
    }, 5000 );

The server checks whether the incoming token is in the online users list and sends the status to the special room for the user who sent this request.

    socket.on('online_status', ( data ) => {
        if ( Object.values(users).indexOf(data.other_user_token) !== -1 ) {
            data.status = true;
            io.emit(data.user_token+'online_status', data );
        } else {
            data.status = false;
            io.emit(data.user_token+'online_status', data );
        }
    });

But still, the user connected to the socket can see the data flow from the browser console.

I am "io.emit ('user_2', data);" When I use it, shouldn't it just go to the client where user_2 is listening instead of the whole client?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...