我的节点应用程序使用pm2集群模式,当我尝试通过获取套接字时
// return all Socket instances in the "room1" room of the main namespace
const sockets = await io.in("room1").fetchSockets();
有时我得到了实际的套接字,我附加了一些自定义数据事件元数据,有时我得到的远程套接字没有我的自定义数据,请在下面找到我得到的实际套接字和远程套接字
1|test-app | sockets--- [
1|test-app | Socket {
1|test-app | _events: [Object: null prototype] {
1|test-app | error: [AsyncFunction (anonymous)],
1|test-app | req: [Function: bound requestHandler] AsyncFunction,
1|test-app | disconnect: [Function (anonymous)]
1|test-app | },
1|test-app | _eventsCount: 3,
1|test-app | _maxListeners: undefined,
1|test-app | nsp: Namespace {
1|test-app | _events: [Object: null prototype],
1|test-app | _eventsCount: 1,
1|test-app | _maxListeners: undefined,
1|test-app | sockets: [Map],
1|test-app | _fns: [],
1|test-app | _ids: 0,
1|test-app | server: [Server],
1|test-app | name: '/',
1|test-app | adapter: [RedisAdapter],
1|test-app | [Symbol(kCapture)]: false
1|test-app | },
1|test-app | client: Client {
1|test-app | sockets: [Map],
1|test-app | nsps: [Map],
1|test-app | server: [Server],
1|test-app | conn: [Socket],
1|test-app | encoder: Encoder {},
1|test-app | decoder: [Decoder],
1|test-app | id: 'pIQW8CCYn82_GSpoAAAP',
1|test-app | onclose: [Function: bound onclose],
1|test-app | ondata: [Function: bound ondata],
1|test-app | onerror: [Function: bound onerror],
1|test-app | ondecoded: [Function: bound ondecoded],
1|test-app | connectTimeout: undefined
1|test-app | },
1|test-app | data: {},
1|test-app | acks: Map(0) {},
1|test-app | fns: [ [AsyncFunction (anonymous)] ],
1|test-app | flags: {},
1|test-app | server: Server {
1|test-app | _events: [Object: null prototype] {},
1|test-app | _eventsCount: 0,
1|test-app | _maxListeners: undefined,
1|test-app | _nsps: [Map],
1|test-app | parentNsps: Map(0) {},
1|test-app | _path: '/socket.io',
1|test-app | clientPathRegex: /^/socket.io/socket.io(.min|.msgpack.min)?.js(.map)?$/,
1|test-app | _connectTimeout: 45000,
1|test-app | _serveClient: true,
1|test-app | _parser: [Object],
1|test-app | encoder: Encoder {},
1|test-app | _adapter: [Function (anonymous)],
1|test-app | sockets: [Namespace],
1|test-app | opts: [Object],
1|test-app | eio: [Server],
1|test-app | httpServer: [Server],
1|test-app | engine: [Server],
1|test-app | [Symbol(kCapture)]: false
1|test-app | },
1|test-app | adapter: RedisAdapter {
1|test-app | _events: [Object: null prototype] {},
1|test-app | _eventsCount: 0,
1|test-app | _maxListeners: undefined,
1|test-app | nsp: [Namespace],
1|test-app | rooms: [Map],
1|test-app | sids: [Map],
1|test-app | encoder: Encoder {},
1|test-app | pubClient: [RedisClient],
1|test-app | subClient: [RedisClient],
1|test-app | requests: Map(0) {},
1|test-app | uid: 'dh0K01',
1|test-app | requestsTimeout: 5000,
1|test-app | publishOnSpecificResponseChannel: false,
1|test-app | channel: 'socket.io#/#',
1|test-app | requestChannel: 'socket.io-request#/#',
1|test-app | responseChannel: 'socket.io-response#/#',
1|test-app | [Symbol(kCapture)]: false
1|test-app | },
1|test-app | id: 'pIQW8CCYn82_GSpoAAAP',
1|test-app | connected: true,
1|test-app | disconnected: false,
1|test-app | handshake: {
1|test-app | headers: [Object],
1|test-app | time: 'Fri Jul 08 2022 05:48:10 GMT+0000 (Coordinated Universal Time)',
1|test-app | address: '::ffff:10.30.11.223',
1|test-app | xdomain: false,
1|test-app | secure: false,
1|test-app | issued: 1657259290376,
1|test-app | url: '/socket.io/?EIO=3&transport=websocket',
1|test-app | query: [Object: null prototype],
1|test-app | auth: {}
1|test-app | },
1|test-app | eventMetaData: {
1|test-app | userObjectId: '12345',
1|test-app | },
1|test-app | [Symbol(kCapture)]: false
1|test-app | }
1|test-app | ]
```
and remote socket sometimes
2|test-app | sockets--- [
2|test-app | RemoteSocket {
2|test-app | id: 'XCqoMchtLYdsB9ldAAAJ',
2|test-app | handshake: {
2|test-app | headers: [Object],
2|test-app | time: 'Fri Jul 08 2022 06:10:13 GMT+0000 (Coordinated Universal Time)',
2|test-app | address: '::ffff:10.30.1.185',
2|test-app | xdomain: false,
2|test-app | secure: false,
2|test-app | issued: 1657260613462,
2|test-app | url: '/socket.io/?EIO=3&transport=websocket',
2|test-app | query: [Object],
2|test-app | auth: {}
2|test-app | },
2|test-app | rooms: Set(2) {
2|test-app | 'XCqoMchtLYdsB9ldAAAJ',
2|test-app | 'ef88ce1b91524264bbb020bcafe6b61c'
2|test-app | },
2|test-app | data: {},
2|test-app | operator: BroadcastOperator {
2|test-app | adapter: [RedisAdapter],
2|test-app | rooms: [Set],
2|test-app | exceptRooms: Set(0) {},
2|test-app | flags: {}
2|test-app | }
2|test-app | }
2|test-app | ]
带有
socketClient.sockets.sockets.get(socketId)
在RemoteSocket的情况下,当没有返回实际的套接字时,我得到了null如何每次在pm2集群中获得实际的套接字。。???套接字和远程套接字之间的区别是什么(见套接字数组的第一个元素(
我建议在套接字数据对象中设置eventMetaData对象,例如:
socket.data.eventMetaData = {userObjectId: '12345'}
然后,您可以使用再次访问数据
// Get the sockets from server server cluster
const sockets = await io.in("room1").fetchSockets();
// Find the socket you want in the room (and check sockets isn't zero length)
if (sockets && sockets.length > 0) {
// Going to assume you want the first socket in the room
const socket = sockets[0];
const metadata = socket.data.eventMetaData;
}
else {
// Didn't get any sockets for the room - handle that
}
此处描述:https://socket.io/docs/v4/server-instance/#fetchsockets