SOCKET.IO - 在同一登录用户的两个不同浏览器中使用socket.id

时间:2015-01-20 20:52:30

标签: javascript node.js socket.io

这是一个关于在您想要为一个用户触发套接字事件的场景中该怎么做的问题,可能会被记录到另一个浏览器中。

我有几个功能可以更新用户'工作堆实时(在其他用户可分配的其他工作堆的队列中);但是,如果用户同时登录到另一个浏览器,并在一个浏览器中进行更新,则它不会在另一个浏览器中更新(因为它们具有不同的socket.id)。

我不知道该如何处理...我可以根据登录用户的用户ID来做,但目前我的套接字代码没有任何会话变量的范围,尽管有是会话套接字等模块 - 我不确定这是否是正确的路径。

有人可以建议我这样做吗?

1 个答案:

答案 0 :(得分:1)

如果您不使用任何群集,您可以按照我在Miaou聊天中使用的方法:我只是将用户设置为套接字对象的属性,并在必要时迭代套接字。这允许一些实用的功能。

这是(简化的)相关代码。

io.on('connect', function(socket){
    ...
    var userId = session.passport.user;
    if (!userId) return die("no authenticated user in socket's session");
    ...
    var shoe = new Shoe(socket, completeUser) // <=== bindind user socket here 

    // socket event binding here

Shoe 对象:

// A shoe embeds a socket and is provided to controlers and plugins.
// It's kept in memory by the closures of the socket event handlers
function Shoe(socket, completeUser){
    this.socket = socket;
    this.completeUser = completeUser;
    this.publicUser = {id:completeUser.id, name:completeUser.name};
    this.room;
    socket['publicUser'] = this.publicUser;
    this.emit = socket.emit.bind(socket);
}
var Shoes = Shoe.prototype;



// emits something to all sockets of a given user. Returns the number of sockets
Shoes.emitToAllSocketsOfUser = function(key, args, onlyOtherSockets){
    var currentUserId = this.publicUser.id,
        nbs = 0;
    for (var clientId in io.sockets.connected) {
        var socket = io.sockets.connected[clientId];
        if (onlyOtherSockets && socket === this.socket) continue;
        if (socket && socket.publicUser && socket.publicUser.id===currentUserId) {
            socket.emit(key, args);
            nbs++;
        }
    }
    return nbs;
}

// returns the socket of the passed user if he's in the same room
Shoes.userSocket = function(userIdOrName) {
    var clients = io.sockets.adapter.rooms[this.room.id],
        sockets = [];
    for (var clientId in clients) {
        var socket = io.sockets.connected[clientId];
        if (socket && socket.publicUser && (socket.publicUser.id===userIdOrName||socket.publicUser.name===userIdOrName)) {
            return socket;
        }       
    }
}

// returns the ids of the rooms to which the user is currently connected
Shoes.userRooms = function(){
    var rooms = [],
        uid = this.publicUser.id;
        iorooms = io.sockets.adapter.rooms;
    for (var roomId in iorooms) {
        if (+roomId!=roomId) continue;
        var clients = io.sockets.adapter.rooms[roomId];
        for (var clientId in clients) {
            var socket = io.sockets.connected[clientId];
            if (socket && socket.publicUser && socket.publicUser.id===uid) {
                rooms.push(roomId);
                break;
            }
        }   
    }
    return rooms;
}


// returns the first found socket of the passed user (may be in another room)
function anyUserSocket(userIdOrName) {
    for (var clientId in io.sockets.connected) {
        var socket = io.sockets.connected[clientId];
        if (socket.publicUser && (socket.publicUser.id===userIdOrName||socket.publicUser.name===userIdOrName)) {
            return socket;
        }
    }
}

// closes all sockets from a user in a given room
exports.throwOut = function(userId, roomId, text){
    var clients = io.sockets.adapter.rooms[roomId];;
    for (var clientId in clients) {
        var socket = io.sockets.connected[clientId];
        if (socket.publicUser && socket.publicUser.id===userId) {
            if (text) socket.emit('miaou.error', text);
            socket.disconnect('unauthorized');
        }
    }
}

Real code

现在,基于ES6的节点版本和WeakMap,我可能会实现更直接的映射,但我所描述的解决方案非常强大且足够高效。