Setting up a socket.io room for a specific group b

2019-03-14 04:51发布

I have a web application running on node using express, angular, mongodb and socket.io where users belong to groups. In those groups I need to set up a chat socket whereby only the people who belong to that group can see the messages specific to that group and only send messages to other users in that group. At the moment I have managed to set up the socket.io chat room based on the guidelines on their site, but the messages sent are visible to all other groups. I have read that I need to set up certain rooms, but I am not sure how to set it up so that the room is specific to the group.

The group is held at a domain: for example... www.mydomain.com/groups/groupId/room

The groupId is a unique Id which is generated by MongoDB. I am using Angularjs on the client side, and have a group controller for the groups. If anyone has any ideas how to achieve this or could point me in the right direction I'd be extremely grateful.

My current server side code:

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

io.sockets.on('connection', function(socket) {

    socket.on('connectToServer', function (data) {
        var name = data.name;
        io.sockets.emit('user-join', name + " has connected to the group");
    });

    socket.on('send msg', function (data) {
        io.sockets.emit('get msg', data);
    });

});

and my client side code in the groupController, in which I access a Global service which stores the current user's details is as follows:

var socket = io.connect();

$scope.msgs = [];

$scope.sendMsg = function () {
    socket.emit('send msg', $scope.msg.text);
    $scope.msg.text = "";
}

socket.on('get msg', function (data){
    $scope.msgs.push(data);
});

var currentUser = Global.currentUser();
var name = currentUser.username;

socket.emit('connectToServer', {name: name});

socket.on('user-join', function (data) {
    $scope.msgs.push(data);
});

1条回答
不美不萌又怎样
2楼-- · 2019-03-14 05:13

I'd recommend setting up namespaces for each room you have your chat in. I did something similar in my own code. Note: Rooms and namespaces are a little different from each other in socket.io itself (socket.io has both: http://socket.io/docs/rooms-and-namespaces/).

In the Server code:

I have a method under socket.on('connection') that is similar to

socket.on('groupConnect', function(group){
    var groupNsp = io.of('/' + group);
}

This essentially makes sure that a namespace is exists under the name of the desired one. It doesn't mess it up or reset the namespace when it is called again.

Then for receiving the messages:

socket.on('message', function(data){
    var msg = data.msg;
    var nsp = data.nsp;
    io.of(nsp).emit('message', msg);
}

You could also add the nsp to the data you have already and then just send the data again to the clients.

Then, in the client code:

var socketOut = io.connect('http://yourdomain:1337/');
var someGroupOrMethodToGetGroup;
socketOut.emit('groupConnect', someGroupOrMethodToGetGroup);
var nsp;
setTimeout(function(){
    socket = io.connect('http://yourdomain:1377/' + someGroupOrMethodToGetGroup);
    socket.on('message', function(msg){
        displayMessage(msg);
    }
    nsp = '/' + someGroupOrMethodToGetGroup;
}, 1500);

Then in my displayMessage code I have:

socketOut.emit('message', { msg: desiredMessage, nsp: nsp });
查看更多
登录 后发表回答