Node.js socket.io中的服务器端套接字在';连接';
我在express中使用Socket.io-1.0.6 并尝试下面的代码 服务器Node.js socket.io中的服务器端套接字在';连接';,node.js,namespaces,socket.io,Node.js,Namespaces,Socket.io,我在express中使用Socket.io-1.0.6 并尝试下面的代码 服务器 var io = require('socket.io')(http); io.on('connect', function(socket){ console.log("server nsp->%s", socket.nsp.name); //<-- printed always "server nsp->/", In my thought, it should print
var io = require('socket.io')(http);
io.on('connect', function(socket){
console.log("server nsp->%s", socket.nsp.name);
//<-- printed always "server nsp->/", In my thought, it should print "/custom_nsp".
});
var io=require('socket.io')(http);
io.on('connect',功能(插座){
log(“服务器nsp->%s”,socket.nsp.name);
// 这是因为必须使用
方法的.of执行自定义名称空间处理(请参阅)
如果按照以下方式修改服务器端代码:
io.of('/custom_nsp').on('connect', function(socket) {
console.log("server nsp->%s", socket.nsp.name); //server nsp->/custom_nsp
});
你会得到你所期望的
但仍不清楚为什么即使在连接到/custom\u nsp
时也会触发默认命名空间处理程序。我的尝试是从客户端动态分配自定义命名空间。因此,服务器端.of()对我来说是无用的。@nurinamu AFAIK,你不能使用命名空间。但是,可能会有帮助。客户端可能会发出一个事件(比如,socket.emit('join_-room',{roomName:'custom_-room'})
),服务器将该套接字连接到所需的房间。Thx,我按照您的建议更改了我的例程。
io.of('/custom_nsp').on('connect', function(socket) {
console.log("server nsp->%s", socket.nsp.name); //server nsp->/custom_nsp
});