如何拒绝某些用户加入socket.io中的同一个房间?

时间:2014-05-07 14:04:02

标签: node.js express socket.io

现在我正在使用express4和socket.io 0.9来构建在线聊天室。问题是我不想让同一个用户在浏览器(或其他浏览器)中打开另一个选项卡时加入同一个房间。我可以得到用户会话(访问那些房间,用户必须登录,当他们记录时,'uid'将设置到会话中。)

3 个答案:

答案 0 :(得分:2)

在@Jujuleder的帮助下,我想出了如何解决我的问题。

服务器:

var socketio = require('socket.io'),
    cookieParser = require('cookie-parser')('YOUR-SERECT-KEY'),
    session = require('express-session'),
    RedisStore = require('connect-redis')(session),
    redis = require("redis"),
    client = redis.createClient(6379, "127.0.0.1"),
    store = new RedisStore;

module.exports.listen = function(app){
   var io = socketio.listen(app);
   var pubRoom = new PublicRoom(io);
   pubRoom.listen();
   return io
};

function PublicRoom(io){
   this._baseUrl = '/room/public/';
   this.publicRoom = io.of(this._baseUrl);
}

PublicRoom.prototype.listen = function(){
   this.publicRoom.authorization(function(handshakeData, callback){

   function cookieParserWrapper (handshakeData, next) {
       cookieParser(handshakeData,{}, next);
   }

   cookieParserWrapper(handshakeData,function(){});

   var sid = handshakeData.signedCookies.sid;
   //get the UID from redis
   store.get(sid,function(err,data){
      if(err){
            console.log(err);
       }else{
            handshakeData.uid = data.uid;//set UID into handshake
            callback(null, true);
        }
   });
});

this.publicRoom.on('connection', function(socket){
    socket.on('authReq',function(room,user){
        socket.myID = user;
        var uid = socket.handshake.uid; // We can get the UID here.
        var onlineUsers = self.publicRoom.clients(room);//get users in this room
        if(onlineUsers.length === 0){
            socket.to(room).emit('authRes',true);// Tell the client you can Join
            socket.auth = true;
        }else{
            for(var i= 0,l=onlineUsers.length;i<l;i++){
                if(onlineUsers[i].myID===uid){
                    socket.auth = false;
                    socket.to(room).emit('authRes',false);
                    // Tell the client you can't Join
                }else{
                    socket.auth = true;
                    socket.to(room).emit('authRes',true);// Tell the client you can Join
                }
            }
        }
    });
 });
}

客户端:

socket.on('connect', function() {
    socket.emit('authReq',roomID,userID);
});

答案 1 :(得分:0)

你的问题显然是什么?您无法访问房间中的当前用户?如果是这样,您可以从这样的房间获取插座,然后检查您的用户。

首先在第一个套接字中添加一个参数,以便以后识别。

socket.sessionid=your_user_session_id;

然后,对于每个连接,如果您已经有一个具有此会话ID的套接字,请检查房间的套接字

var socketsOfTheRoom = io.sockets.clients(your_room_name);

for(var i=0;i<socketsOfTheRoom.length;i++){
   sock = socketsOfTheRoom[i]
   if(sock===sessionid_of_new_connection){
      return true
   }
}

return false

答案 2 :(得分:0)

// SERVER.JS

socket.on('authReq',function(room,user, callback){
         if (io.nsps['/'].adapter.rooms[room])
         {
            var onlineUsers = io.sockets.adapter.rooms[room].sockets;
            var totalUsers = io.sockets.adapter.rooms[room];
            if(totalUsers.length === 0){
                callback(true);
            }
            else
            {
                var userinroom = [];
                for (var id in onlineUsers) {
                  if (io.of('/').adapter.nsp.connected[id].username==user)
                      userinroom.push(user);                      
                }               
                if (userinroom.length==0) callback(true);
                else callback(false);
            }
         }
         else
            callback(true);
    });

// CLIENT.JS

socket.emit('authReq',roomname,username, function(callback) {
                    if(callback) 
                        if (username) socket.emit('adduser', username, rooname);
                });