socket.io将消息从一个命名空间发送到另一个命名空间

socket.io将消息从一个命名空间发送到另一个命名空间,socket.io,namespaces,emit,Socket.io,Namespaces,Emit,我正在尝试将消息从一个命名空间发送到另一个命名空间(在连接上)。 下面是一些示例代码,说明了我是如何尝试实现它的 [服务器] // Namespaces var users_ns = io.of('/users'); var machines_ns = io.of('/machines'); // Attempt to receive the event on the socket users_ns.on('connection', function(socket){ socket.o

我正在尝试将消息从一个命名空间发送到另一个命名空间(在连接上)。 下面是一些示例代码,说明了我是如何尝试实现它的

[服务器]

// Namespaces
var users_ns = io.of('/users');
var machines_ns = io.of('/machines');

// Attempt to receive the event on the socket
users_ns.on('connection', function(socket){
    socket.on('test', function(socket){
        console.log('socket test');
    });
});

// Attempt to receive the event on the namespace
users_ns.on('test', function(socket){
    console.log('namespace test');
});

// Emit an event to the 'users' namespace
machines_ns.on('connection', function(socket){
    users_ns.emit('test');
});
var socket = io('http://localhost/users');
var socket = io('http://localhost/machines');
// Namespaces
var users_ns = io.of('/users');
var machines_ns = io.of('/machines');

// Attempt to receive the event on the socket
users_ns.on('connection', function(socket){
    socket.on('test', function(){
        console.log('socket test');
    });
});


// Emit an event to the 'users' namespace
machines_ns.on('connection', function(socket){
    users_ns.emit('test');
});
var socket = io('http://localhost/users');
socket.on('test',function(){ alert('broadcast received');});
var socket = io('http://localhost/machines');
[Client1]

// Namespaces
var users_ns = io.of('/users');
var machines_ns = io.of('/machines');

// Attempt to receive the event on the socket
users_ns.on('connection', function(socket){
    socket.on('test', function(socket){
        console.log('socket test');
    });
});

// Attempt to receive the event on the namespace
users_ns.on('test', function(socket){
    console.log('namespace test');
});

// Emit an event to the 'users' namespace
machines_ns.on('connection', function(socket){
    users_ns.emit('test');
});
var socket = io('http://localhost/users');
var socket = io('http://localhost/machines');
// Namespaces
var users_ns = io.of('/users');
var machines_ns = io.of('/machines');

// Attempt to receive the event on the socket
users_ns.on('connection', function(socket){
    socket.on('test', function(){
        console.log('socket test');
    });
});


// Emit an event to the 'users' namespace
machines_ns.on('connection', function(socket){
    users_ns.emit('test');
});
var socket = io('http://localhost/users');
socket.on('test',function(){ alert('broadcast received');});
var socket = io('http://localhost/machines');
[Client2]

// Namespaces
var users_ns = io.of('/users');
var machines_ns = io.of('/machines');

// Attempt to receive the event on the socket
users_ns.on('connection', function(socket){
    socket.on('test', function(socket){
        console.log('socket test');
    });
});

// Attempt to receive the event on the namespace
users_ns.on('test', function(socket){
    console.log('namespace test');
});

// Emit an event to the 'users' namespace
machines_ns.on('connection', function(socket){
    users_ns.emit('test');
});
var socket = io('http://localhost/users');
var socket = io('http://localhost/machines');
// Namespaces
var users_ns = io.of('/users');
var machines_ns = io.of('/machines');

// Attempt to receive the event on the socket
users_ns.on('connection', function(socket){
    socket.on('test', function(){
        console.log('socket test');
    });
});


// Emit an event to the 'users' namespace
machines_ns.on('connection', function(socket){
    users_ns.emit('test');
});
var socket = io('http://localhost/users');
socket.on('test',function(){ alert('broadcast received');});
var socket = io('http://localhost/machines');

你知道为什么这不起作用吗?

你的服务器代码是正确的,但是发生了一些误解

[服务器]

// Namespaces
var users_ns = io.of('/users');
var machines_ns = io.of('/machines');

// Attempt to receive the event on the socket
users_ns.on('connection', function(socket){
    socket.on('test', function(socket){
        console.log('socket test');
    });
});

// Attempt to receive the event on the namespace
users_ns.on('test', function(socket){
    console.log('namespace test');
});

// Emit an event to the 'users' namespace
machines_ns.on('connection', function(socket){
    users_ns.emit('test');
});
var socket = io('http://localhost/users');
var socket = io('http://localhost/machines');
// Namespaces
var users_ns = io.of('/users');
var machines_ns = io.of('/machines');

// Attempt to receive the event on the socket
users_ns.on('connection', function(socket){
    socket.on('test', function(){
        console.log('socket test');
    });
});


// Emit an event to the 'users' namespace
machines_ns.on('connection', function(socket){
    users_ns.emit('test');
});
var socket = io('http://localhost/users');
socket.on('test',function(){ alert('broadcast received');});
var socket = io('http://localhost/machines');
当您向用户广播\u ns套接字时,此事件在客户端接收,而不是在服务器端。所以这是正确的客户端代码

[Client1]

// Namespaces
var users_ns = io.of('/users');
var machines_ns = io.of('/machines');

// Attempt to receive the event on the socket
users_ns.on('connection', function(socket){
    socket.on('test', function(socket){
        console.log('socket test');
    });
});

// Attempt to receive the event on the namespace
users_ns.on('test', function(socket){
    console.log('namespace test');
});

// Emit an event to the 'users' namespace
machines_ns.on('connection', function(socket){
    users_ns.emit('test');
});
var socket = io('http://localhost/users');
var socket = io('http://localhost/machines');
// Namespaces
var users_ns = io.of('/users');
var machines_ns = io.of('/machines');

// Attempt to receive the event on the socket
users_ns.on('connection', function(socket){
    socket.on('test', function(){
        console.log('socket test');
    });
});


// Emit an event to the 'users' namespace
machines_ns.on('connection', function(socket){
    users_ns.emit('test');
});
var socket = io('http://localhost/users');
socket.on('test',function(){ alert('broadcast received');});
var socket = io('http://localhost/machines');
[Client2]

// Namespaces
var users_ns = io.of('/users');
var machines_ns = io.of('/machines');

// Attempt to receive the event on the socket
users_ns.on('connection', function(socket){
    socket.on('test', function(socket){
        console.log('socket test');
    });
});

// Attempt to receive the event on the namespace
users_ns.on('test', function(socket){
    console.log('namespace test');
});

// Emit an event to the 'users' namespace
machines_ns.on('connection', function(socket){
    users_ns.emit('test');
});
var socket = io('http://localhost/users');
var socket = io('http://localhost/machines');
// Namespaces
var users_ns = io.of('/users');
var machines_ns = io.of('/machines');

// Attempt to receive the event on the socket
users_ns.on('connection', function(socket){
    socket.on('test', function(){
        console.log('socket test');
    });
});


// Emit an event to the 'users' namespace
machines_ns.on('connection', function(socket){
    users_ns.emit('test');
});
var socket = io('http://localhost/users');
socket.on('test',function(){ alert('broadcast received');});
var socket = io('http://localhost/machines');

当一个套接字连接到计算机名称空间时,所有连接到用户名称空间的客户端都会发出“广播已接收”警报。

太好了,非常感谢您的回复!这完全有道理。我的困惑是,我认为事件应该先通过其他NS,然后才最终发送到客户端。