Socket.io 中的服务器端套接字在"连接"时返回错误的命名空间



我正在使用Socket。IO - 1.0.6 with express.

并尝试下面的代码。

服务器

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 socket = io.connect("http://mysocket.io/custom_nsp");
socket.on('connect', function(){
     console.log("client nsp->%s", socket.nsp);   
     //<-- printed correctly "client nsp->/custom_nsp"
});

我不知道为什么服务器套接字的命名空间总是"/"。

有什么不对吗?

这是因为自定义名称空间处理必须使用.of方法执行(参见文档)。

如果按照如下方式修改服务器端代码:

io.of('/custom_nsp').on('connect', function(socket) {
    console.log("server nsp->%s", socket.nsp.name);  //server nsp->/custom_nsp
});

你会得到你想要的。

但是仍然不清楚为什么默认的命名空间处理程序即使在连接到/custom_nsp时也会触发

最新更新