我想将所有套接字的会话检索到房间中。
我与express-session
在socket.io会话中共享了express-socket.io-session
。
我的仆人:
import * as sharedsession from 'express-socket.io-session';
import * as session from 'express-session';
import * as connectRedis from 'connect-redis';
import * as express from 'express';
import * as SocketIO from 'socket.io';
import * as redisSocket from 'socket.io-redis';
// redis optinon
const redisOption = { port: 'XXX' };
// express.js
const app = express();
// create the redis store for the express session
const RedisStore = connectRedis(session);
const expressSession = session({
name: 'SESSIONID',
store: new RedisStore(redisOption),
secret: 'as54f6a65sf4fa',
resave: false,
saveUninitialized: false,
cookie: { secure: true }
});
// set the session into express
app.use(expressSession);
// socket.io
const io = SocketIO(httpServer, {path: '/api/socket.io', origins: '*:*'});
// se the adapter for socket io
io.adapter(redisSocket(redisOption));
// share the express session with socket io
io.use(sharedsession(expressSession, {autoSave: true}));
// just for test, join all socket into a room 'foo'
io.on('connection', (client) => {
client.join('foo');
});
// start the express server
app.listen(3000);
我试图将用户会话检索到会议室中
io.in('foo').clients((err, clients) => {
for (let i = 0, e = clients.length; i < e; i++) {
const client = clients[i]; // socket id
// HOW GET THE SOCKET SESSION?
}
});
我也尝试过
// this works only for the current instance, don't return all sockets into the redis adapter
const sockets = io.sockets.adapter.rooms['foo'].sockets;