Socketio-join与Redis-subscribe有什么不同?

时间:2019-12-10 23:14:43

标签: node.js express socket.io passport.js

我正在使用以下方法在节点js中创建应用程序:

    "socket.io":"~2.3.0",
    "redis":"~2.8.0",
    "connect-redis":"4.0.3",
    "passport.socketio":"~3.7.0"

,我不知道如何注册用户的会话。为什么这样因为我希望用户在收到通知时将其发送到相同的所有会话。您可以使用“加入”并在用户连接时添加它,或将其订阅到Redis频道

这是我的代码:


var sockets = {};

sockets.init = function (server, sessionStore, passport, cookieParser) {
    // socket.io setup
    var passportSocketIo = require('passport.socketio');

    var io = require('socket.io').listen(server);
    var redis = require('redis');
    var redisClient = redis.createClient({
        port      : 6379,               
        host      : process.env.DOMAIN,        
    });

    io.use(passportSocketIo.authorize({
        key: 'connect.sid',
        secret: "AAAAAA",
        store: sessionStore,
        passport: passport,
        cookieParser: cookieParser
    }));

    var eventSocket = io.of('/events');
// on connection event
eventSocket.on('connection', function(socket) {
    //this my plan with join 
    socket.join('user_room_'+socket.request.user._id);
});
}

module.exports = sockets;

0 个答案:

没有答案