我想检索一个房间中连接的服务器端所有套接字。
我发现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 session
const session = session({
name: 'SESSIONID',
store: new RedisStore(redisOption),
secret: 'Ashg8hV77bxFeyLVec',
resave: false,
saveUninitialized: false
});
app.use(session);
// start express server
const httpServer = app.listen(80);
// attach the express app to socket.io server
const socketServer = SocketIO(httpServer, { path: '/api/socket.io', origins: '*:*' });
// set redis as socket adpter for multi instance/nodes
socketServer.adapter(redisSocket(redisOption));
// share the express session with socket.io
socketServer.use(sharedsession(session, {autoSave: true}));
// 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???
}
});
但我需要检索所有套接字会话/握手。
有一种方法可以检索所有套接字会话/握手吗?
旁注:套接字服务器是带有 socket.io-redis 的多实例/节点
- 套接字.io:2.3.0
- socket.io-redis: 5.2.0