为了账号安全,请及时绑定邮箱和手机立即绑定

socket.io 和 socket.io-redis:获取房间中的所有套接字对象

socket.io 和 socket.io-redis:获取房间中的所有套接字对象

有只小跳蛙 2022-01-07 13:25:27
我想检索一个房间中连接的服务器端所有套接字。我发现clients如果在方法之后链接in返回房间中连接的所有套接字的方法:import * as express from 'express';import * as SocketIO from 'socket.io';import * as redisSocket from 'socket.io-redis';import * as sharedsession from 'express-socket.io-session';const app = express();const redisOption = {port: 6379, host: 'XXX'};// use the RedisStore as express sessionconst session = session({  name: 'SESSIONID',  store: new RedisStore(redisOption),  secret: 'Ashg8hV77bxFeyLVec',  resave: false,  saveUninitialized: false});app.use(session);// start express serverconst httpServer = app.listen(80);// attach the express app to socket.io serverconst socketServer = SocketIO(httpServer, { path: '/api/socket.io', origins: '*:*' });// set redis as socket adpter for multi instance/nodessocketServer.adapter(redisSocket(redisOption));// share the express session with socket.iosocketServer.use(sharedsession(session, {autoSave: true}));// get all client in a roomsocketServer.in(room_id).clients((err, clients) => {    for (let i = 0, e = clients.length; i < e; i++) {        const client = clients[i];        console.log(client); // print the socket id        // HOW RETRIVE THE SOCKET OBJECT???    }});但我需要检索所有套接字会话/握手。有一种方法可以检索所有套接字会话/握手吗?旁注:套接字服务器是带有 socket.io-redis 的多实例/节点套接字.io:2.3.0socket.io-redis: 5.2.0
查看完整描述

2 回答

?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

我不确定下面的代码是否有效,但我认为通过使用 socket.io-redis 提供的 customHook,我们可以获得基于 redis 的多节点的 socket.handshake.session。


我希望下面的代码有所帮助。


// get all client in a room

socketServer.in(room_id).clients((err, clients) => {

    for (let i = 0, e = clients.length; i < e; i++) {

        const client = clients[i];

        console.log(client); // print the socket id


        // HOW RETRIVE THE SOCKET OBJECT???

    }

});


// set root namespace

const rootNamespace = socketServer.of('/');


// define customHook

rootNamespace.adapter.customHook = (request, cb) => {

    // every socket.io server execute below, when customRequest requested

    const type = request.type;

    if(type === 'getHandShakeSession'){

        // get all socket objects on local socket.io server

        const sockets = rootNamespace.connected;

        const socketIDs = Object.keys(sockets);

        // get all socket.handshak.session array on local socket.io server

        const sessions = socketIDs.map(socketID => sockets[socketID].handshake.session);

        cb(sessions)

    }

    cb()

}


// request customRequest

rootNamespace.adapter.customRequest({type:'getHandShakeSession'},(err,replies) => {

    //replies are array which element was pushed by cb(element) on individual socket.io server

    //remove empty reply 

    const filtered = replies.filter(reply => reply !== undefined)

    // filtered seems like [[session1,session2,...],[sesssion3,session4,...],..]

    console.log(filtered)

} )


查看完整回答
反对 回复 2022-01-07
?
当年话下

TA贡献1890条经验 获得超9个赞

Object.keys(io.sockets.sockets); 它为您提供房间内所有已连接的插座。


查看完整回答
反对 回复 2022-01-07
  • 2 回答
  • 0 关注
  • 450 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信