节点JS获取数据io.sockets.clients();

时间:2019-03-21 02:21:00

标签: javascript jquery node.js socket.io

对不起,我的英语不好

我正在尝试获取所有连接的客户端数据

样本;

var a_sockets           =   io.sockets.clients();

console.log(a_sockets);

返回;

Namespace {
  name: '/',
  server:
   Server {
     nsps: { '/': [Circular] },
     parentNsps: Map {},
     _path: '/socket.io',
     _serveClient: true,
     parser:
      { protocol: 4,
        types: [Array],
        CONNECT: 0,
        DISCONNECT: 1,
        EVENT: 2,
        ACK: 3,
        ERROR: 4,
        BINARY_EVENT: 5,
        BINARY_ACK: 6,
        Encoder: [Function: Encoder],
        Decoder: [Function: Decoder] },
     encoder: Encoder {},
     _adapter: [Function: Adapter],
     _origins: '*:*',
     sockets: [Circular],
     eio:
      Server {
        clients: [Object],
        clientsCount: 1,
        wsEngine: 'ws',
        pingTimeout: 5000,
        pingInterval: 25000,
        upgradeTimeout: 10000,
        maxHttpBufferSize: 100000000,
        transports: [Array],
        allowUpgrades: true,
        allowRequest: [Function: bound ],
        cookie: 'io',
        cookiePath: '/',
        cookieHttpOnly: true,
        perMessageDeflate: [Object],
        httpCompression: [Object],
        initialPacket: [Array],
        ws: [WebSocketServer],
        _events: [Object],
        _eventsCount: 1 },
     httpServer:
      Server {
        _events: [Object],
        _eventsCount: 5,
        _maxListeners: undefined,
        _connections: 1,
        _handle: [TCP],
        _usingWorkers: false,
        _workers: [],
        _unref: false,
        allowHalfOpen: true,
        pauseOnConnect: false,
        httpAllowHalfOpen: false,
        timeout: 120000,
        keepAliveTimeout: 5000,
        _pendingResponseData: 0,
        maxHeadersCount: null,
        headersTimeout: 40000,
        _connectionKey: '6::::9999',
        [Symbol(IncomingMessage)]: [Function],
        [Symbol(ServerResponse)]: [Function],
        [Symbol(asyncId)]: 5 },
     engine:
      Server {
        clients: [Object],
        clientsCount: 1,
        wsEngine: 'ws',
        pingTimeout: 5000,
        pingInterval: 25000,
        upgradeTimeout: 10000,
        maxHttpBufferSize: 100000000,
        transports: [Array],
        allowUpgrades: true,
        allowRequest: [Function: bound ],
        cookie: 'io',
        cookiePath: '/',
        cookieHttpOnly: true,
        perMessageDeflate: [Object],
        httpCompression: [Object],
        initialPacket: [Array],
        ws: [WebSocketServer],
        _events: [Object],
        _eventsCount: 1 } },
  sockets:
   { '71kTaZqD9WLFgyC0AAAA':
      Socket {
        nsp: [Circular],
        server: [Server],
        adapter: [Adapter],
        id: '71kTaZqD9WLFgyC0AAAA',
        client: [Client],
        conn: [Socket],
        rooms: {},
        acks: {},
        connected: true,
        disconnected: false,
        handshake: [Object],
        fns: [],
        flags: {},
        _rooms: [],
        userId:
         '8580af1b5f364c98a20a49185e018412642e92efb79421734881b53e1e1b18b6_t' } },
  connected:
   { '71kTaZqD9WLFgyC0AAAA':
      Socket {
        nsp: [Circular],
        server: [Server],
        adapter: [Adapter],
        id: '71kTaZqD9WLFgyC0AAAA',
        client: [Client],
        conn: [Socket],
        rooms: {},
        acks: {},
        connected: true,
        disconnected: false,
        handshake: [Object],
        fns: [],
        flags: {},
        _rooms: [],
        **userId:
         '8580af1b5f364c98a20a49185e018412642e92efb79421734881b53e1e1b18b6_t'** } },
  fns: [],
  ids: 0,
  rooms: [],
  flags: {},
  adapter:
   Adapter {
     nsp: [Circular],
     rooms: { '71kTaZqD9WLFgyC0AAAA': [Room] },
     sids: { '71kTaZqD9WLFgyC0AAAA': [Object] },
     encoder: Encoder {} },
  _events: [Object: null prototype] { connection: [Function] },
  _eventsCount: 1 }

只想获取userId数据。

我想带它[Connected-> userId]

所以我可以比较用户 我怎么解决这个问题? 我再次为我的英语道歉

谢谢。

如果您能帮助我,我将非常高兴。 祝你有美好的一天

1 个答案:

答案 0 :(得分:0)

简短答案(假设您使用的版本> = 1.0):

var srvSockets = io.sockets.sockets;
Object.keys(srvSockets);