Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

node.js - how to list rooms on socket.io nodejs server

after progress on the question how to create socket.io multicast groups, I found making rooms a great way to do what I needed.

However, it would be great to know about all the rooms, without an extra data-structure.

Is it possible to get a list of all rooms on the server from the server socket?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The short answer:

io.sockets.adapter.rooms

I analysed io:

I got the following output:

{ server: 
   { stack: 
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object] ],
     connections: 3,
     allowHalfOpen: true,
     watcher: { host: [Circular], callback: [Function] },
     _events: 
      { request: [Function],
        connection: [Function: connectionListener],
        listening: [Object],
        upgrade: [Object] },
     httpAllowHalfOpen: false,
     cache: {},
     settings: { home: '/', env: 'development', hints: true },
     redirects: {},
     isCallbacks: {},
     _locals: { settings: [Object], app: [Circular] },
     dynamicViewHelpers: {},
     errorHandlers: [],
     route: '/',
     routes: 
      { app: [Circular],
        routes: [Object],
        params: {},
        _params: [],
        middleware: [Function] },
     router: [Getter],
     __usedRouter: true,
     type: 'tcp4',
     fd: 7 },
  namespaces: 
   { '': 
      { manager: [Circular],
        name: '',
        sockets: [Object],
        auth: false,
        flags: [Object],
        _events: [Object] } },
  sockets: 
   { manager: [Circular],
     name: '',
     sockets: { '210837319844898486': [Object] },
     auth: false,
     flags: { endpoint: '', exceptions: [] },
     _events: { connection: [Function] } },
  settings: 
   { origins: '*:*',
     log: true,
     store: 
      { options: undefined,
        clients: [Object],
        manager: [Circular] },
     logger: { colors: true, level: 1 },
     heartbeats: true,
     resource: '/socket.io',
     transports: 
      [ 'websocket',
        'htmlfile',
        'xhr-polling',
        'jsonp-polling' ],
     authorization: [Function],
     'log level': 1,
     'close timeout': 25,
     'heartbeat timeout': 15,
     'heartbeat interval': 20,
     'polling duration': 20,
     'flash policy server': true,
     'flash policy port': 843,
     'destroy upgrade': true,
     'browser client': true,
     'browser client minification': false,
     'browser client etag': false,
     'browser client handler': false,
     'client store expiration': 15 },
  handshaken: 
   { '210837319844898486': 
      { headers: [Object],
        address: [Object],
        time: 'Mon Jul 18 2011 00:53:27 GMT+0200 (CEST)',
        xdomain: false,
        secure: undefined,
        PHPSESSID: '7qo6cht3q0rskhfes4eesb2d05' } },
  connected: { '210837319844898486': true },
  open: { '210837319844898486': true },
  closed: {},
  closedA: [],
  rooms: 
   { '': [ '210837319844898486' ],
     '/public-alfred': [ '210837319844898486' ] },
  roomClients: { '210837319844898486': [ '': true, '/public-alfred': true ] },
  oldListeners: [ [Function] ],
  _events: 
   { 'set:origins': [Function],
     'set:flash policy port': [Function],
     'set:transports': [Function] } }

after joining room "public-alfred" from a single client io.sockets.adapter.rooms contained:

 { '': [ '210837319844898486' ],
         '/public-alfred': [ '210837319844898486' ] }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...