【问题标题】:socket.io switching namespacessocket.io 切换命名空间
【发布时间】:2018-06-10 17:47:58
【问题描述】:

我目前正在与 socket.io 进行简单的聊天。基础知识已经开始工作了,但现在我正在尝试实现 2 个不同的命名空间。我希望客户端能够通过单击按钮从一个命名空间(支持聊天)切换到另一个命名空间(朋友聊天)。

服务器端

//default namespace
io.on('connection', function(socket){
    console.log('a user connected to the chat');

    socket.on('disconnect', function(){
        console.log('user disconnected');
    });

    socket.on('client message', function(msg){
        io.emit('server_message', msg);
    });
});

//namespace /support
var sup = io.of('/support');
sup.on('connection', function(socket){
    console.log('someone entered the support-chat');

    socket.on('disconnect', function(){
        console.log('user disconnected from support-chat');
    });

    //recieving and emitting message to all clients in namespace /support
    socket.on('client message', function(msg){
        console.log('message received: ' + msg);
        io.of('/support').emit('server_message', msg);
    });
});

//namespace /friends
var frnd = io.of('/friends');
frnd.on('connection', function(socket){
    console.log('someone entered the friends-chat');

    socket.on('disconnect', function(){
        console.log('user disconnected from friends-chat');
    });

    //recieving and emitting message to all clients in namespace /friends
    socket.on('client message', function(msg){
        console.log('message received: ' + msg);
        io.of('/friends').emit('server_message', msg);
    });
});

客户端

var socket = io.connect();
//toggle namespace
            $("#support_button").click(function(){
                socket.disconnect();
                socket = io('/support');
                $('#messages').append($('<li>').text("You entered the Support-Chat"));
            });
//toggle namespace
            $("#friends_button").click(function(){
                socket.disconnect();
                socket = io('/friends');
                $('#messages').append($('<li>').text("You entered the Friends-Chat"));
            });
//sending message on submit
            $('form').submit(function(){
                socket.emit('client message', $('#m').val());
                $('#m').val('');
                return false;
            });
//recieving message and display
            socket.on('server_message', function(msg){
                $('#messages').append($('<li>').text(msg));
            });
        });

我认为开关本身正在工作,因为连接和断开连接事件正在按应有的方式触发。但是当向同一个命名空间中的每个人发送消息(服务器已经从客户端收到)时,它就不起作用了。

这不是在特定命名空间中发出的服务器端调用吗?:

io.of('namespace').emit();

我是否误解了命名空间的用法?我想在 2 个主聊天的名称空间“拆分”之后立即实施房间,以获得支持和朋友。 还是我在服务器端错误地实现了命名空间?我认为 io.on(..)、io.of('/support').on(..) 和 io.of('/friends').on(..) 都以相同的方式工作并抓住他们自己的命名空间客户端的事件。

非常感谢任何帮助!我觉得命名空间在“基本使用”文档中被忽略了。

【问题讨论】:

    标签: node.js sockets socket.io namespaces


    【解决方案1】:

    您不能在现有连接上“切换”命名空间。建立连接后,您连接到特定的命名空间,一旦建立,就无法更改。

    您可以删除当前连接并使用新连接连接到新命名空间。但是,给定您的应用程序,如果您想切换命名空间并且应该使用房间,那么您正在滥用命名空间的概念。

    对于房间,客户端可以向服务器发送切换房间的请求,然后服务器可以将用户从现有房间中删除并将其添加到新房间。然后,从服务器,您可以轻松地向给定房间中的所有连接广播。

    事实上,聊天室是围绕聊天的概念而发明的(尽管它们还有许多其他用途),因此它们非常适合您希望实现的聊天室。

    命名空间是比房间更重的权重划分。建立连接时,连接必须连接到特定的命名空间,并且在连接期间不能更改。

    另一方面,房间要灵活得多。服务器可以随时将给定的连接添加或删除房间中的连接,并且连接甚至可以在多个房间中。

    房间和命名空间都支持向该集合中的所有用户广播。

    我认为命名空间更像是功能通道。所以,我想连接到“价格”更改命名空间以获得价格变化的通知,或者我想连接到“系统”命名空间以获得有关系统中发生的事情的警报或发送消息到管理系统中的事物。

    鉴于房间是对共享信息感兴趣的用户的任意集合,而我可能在多个房间中。

    【讨论】:

    • 这是一个很好的答案,正是我需要知道的!非常感谢!
    猜你喜欢
    • 2012-06-11
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    相关资源
    最近更新 更多