Socket.io shares socket data between namespaces

I want to know how to exchange data installed on a socket in one namespace and access it in another namespace?

I fully understand that I can attach data to the socket object itself. The problem occurs when I attach data to one namespace and try to access it in another namespace.

The problem is shown below.

var io = require( 'socket.io' );

module.exports.init = function( server ) {

    io = io.listen( server );

    io.of( '/chatSystem' ).on( 'connection', function( socket ) {
        /*handling set nickname event*/
        socket.on( 'set.name', function( data ) {
            /*attach nickname key to this socket*/
            socket.nickname = data.name;
            socket.broadcast.emit( 'user.entered', data );
        });
    });

    io.of( '/chatUser').on( 'connection', function( socket ) {
        /*handling client event socket.send*/
        socket.on( 'message', function( data ) {
            data = JSON.parse( data );
            data.nickname = socket.nickname; // <--- this is undefined

            /*send to all connected client( broadcast will not send message to socket that created the message)*/
            socket.broadcast.send( JSON.stringify( data ) );
            data.type = 'myMessage';

            /*manually send back the message to the socket that created the message*/
            socket.send( JSON.stringify( data) );
        });
    });
};

Is there any way to fix this?

+4
source share
3 answers

It may be a little late, but I just stumbled upon a simpler solution and thought that I would share for future researchers.

socket > 1.0, - . , -

    /* attach nickname key to this socket */
    socket.client.nickname = data.name;
    /* In typescript with @types/socket.io */
    socket.client["nickname"] = data.name;

    data.nickname = socket.client.nickname;
    /* In typescript with @types/socket.io */
    data.name = socket.client["nickname"];

, -. . .

+2

""

, , , .

, : namespace_A namespace_B.

namepsace_A namespace_B, > ID ( ) namespace_A.

, , , , .

socket.id . : /namespace#vCpuzzUi4RI1DHqfAAAH. , namespace, i. , namespace_A, - /namespace_A#vCpuzzUi4RI1DHqfAAAH, namespace_B - /namespace_B#vCpuzzUi4RI1DHqfAAAH. , , , .

, :

var self = this;

. namespace_A, namespace_B, A:

ns_b_socket = self.namespace_B.connected['/namespace_B#' + buildSocketID(socket.id)];

namespace_B , self.namespace_B, .

# :

Hash of Socket, , .

.

function buildSocketID(iden) {
    return iden.split('#')[1];  
}

, ns_b_socket.username namespace_A, , socket.username, namespace_B.

.. 1.0.x . , .

+1
, .
var io = require( 'socket.io' );

module.exports.init = function (server) {

 /**
     * Hi This is where things, get tricky now.
     * statement of Problem: Am trying to send message between two namespaces.
     * first made individual namespace socket global,
     * And create a function that will utilized these global sockets as their second arguments
     * and make calls to them
     */

io = io.listen( server );
var chatSystemPresentSocket; // making it Global
var chatUserPresentSocket;  //making it Global

io.of( '/chatSystem' ).on( 'connection', function( socket ) {
    /*handling set nickname event*/
    socket.on( 'set.name', function( data ) {
        /*attach nickname key to this socket*/
        //socket Global for the moment.
        chatSystemPresentSocket = socket;

        sendtochatSystem(data) // function calls
        socket.nickname = data.name;
        socket.broadcast.emit( 'user.entered', data );
    });
});

io.of( '/chatUser').on( 'connection', function( socket ) {
    /*handling client event socket.send*/
    socket.on( 'message', function( data ) {
        /* make socket global for the moment.*/
        chatUserPresentSocket = socket;
        sendtochatSystem(data);
        data = JSON.parse( data );
        data.nickname = socket.nickname; // <--- this is undefined

        /*send to all connected client( broadcast will not send message to socket that created the message)*/
        socket.broadcast.send( JSON.stringify( data ) );
        data.type = 'myMessage';

        /*manually send back the message to the socket that created the message*/
        socket.send( JSON.stringify( data) );
    });
});

//Make a function to use one of the global sockets of one of the namespaces
function sendToChatUser(data, chatUserPresentSocket){
chatUserPresentSocket.emit('your event', data)
}

function sendToChatSystem(data, chatSystemPresentSocket){
chatSystemPresentSocket.emit('your event', data)
}

};

Test it if it doesn't fit, do you know where to edit in my code

0
source

Source: https://habr.com/ru/post/1584262/


All Articles