Socket.io (v1.2)の同時アクセス数を取得するミニマム構成とSocketの中身


同時アクセス数(同時接続数/同時コネクション数)の負荷テストをする準備です。
同時アクセス数を表示出来るようにします。

##環境
* Node.js 0.10.33
* socket.io 1.2

![](http://i.gyazo.com/99aa15101a2d709b8632ba5972b447fe.gif)


## ソースコード

```js
//httpサーバー立ち上げ
var fs = require('fs');
var app = require('http').createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(fs.readFileSync('load.html'));
}).listen(4000);

console.log('接続数取得テスト');

//Socket.ioコネクション
var io = require('socket.io').listen(app);
io.sockets.on('connection', function(socket) {

  console.log('コネクション数',socket.client.conn.server.clientsCount);
  io.sockets.emit('count', socket.client.conn.server.clientsCount);

  socket.on('disconnect', function(data) {
    console.log('コネクション数',socket.client.conn.server.clientsCount);
    io.sockets.emit('count', socket.client.conn.server.clientsCount);
  });
});
```

```load.html
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>

  <p>接続数:<span id="count"></span></p>
  <script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
  <script src="/socket.io/socket.io.js"></script>
  <script>
      var socket = io.connect();
      var $count = $('#count');

      socket.on('count', function(data) {
          $count.text(data);
          console.log(data);
      });
  </script>
</body>
</html>
```

## 実行

```console
$ npm i socket.io socket.io-client
$ node load
```

##変数Socketの中身

上記のload.jsでいうと12行目の`io.sockets.on('connection', function(socket) {`の部分の`socket`変数の中身をメモしておきます。
load.jsでは`socket.client.conn.server.clientsCount`で同時アクセス数を見ていますが、他にも確認出来る変数はいくつかあります。
例えば、`socket.nsp.server.eio.clientsCount`でも確認出来ます。（こっちはengine.io側なのかな...詳細には分かっていません汗）

↓を見ると色々分かりそう。

```js
{ nsp: 
   { name: '/',
     server: 
      { nsps: [Object],
        _path: '/socket.io',
        _serveClient: true,
        _adapter: [Function: Adapter],
        _origins: '*:*',
        sockets: [Circular],
        eio: [Object],
        httpServer: [Object],
        engine: [Object] },
     sockets: [ [Circular] ],
     connected: { '8uWFsXoTCSWbPJETAAAA': [Circular] },
     fns: [],
     ids: 0,
     acks: {},
     adapter: { nsp: [Circular], rooms: [Object], sids: [Object], encoder: {} },
     _events: { connection: [Function] } },
  server: 
   { nsps: { '/': [Object] },
     _path: '/socket.io',
     _serveClient: true,
     _adapter: [Function: Adapter],
     _origins: '*:*',
     sockets: 
      { name: '/',
        server: [Circular],
        sockets: [Object],
        connected: [Object],
        fns: [],
        ids: 0,
        acks: {},
        adapter: [Object],
        _events: [Object] },
     eio: 
      { clients: [Object],
        clientsCount: 1,
        pingTimeout: 60000,
        pingInterval: 25000,
        upgradeTimeout: 10000,
        maxHttpBufferSize: 100000000,
        transports: [Object],
        allowUpgrades: true,
        allowRequest: [Function],
        cookie: 'io',
        ws: [Object],
        _events: [Object] },
     httpServer: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        _connections: 1,
        connections: [Getter/Setter],
        _handle: [Object],
        _usingSlaves: false,
        _slaves: [],
        allowHalfOpen: true,
        httpAllowHalfOpen: false,
        timeout: 120000,
        _connectionKey: '4:0.0.0.0:3000' },
     engine: 
      { clients: [Object],
        clientsCount: 1,
        pingTimeout: 60000,
        pingInterval: 25000,
        upgradeTimeout: 10000,
        maxHttpBufferSize: 100000000,
        transports: [Object],
        allowUpgrades: true,
        allowRequest: [Function],
        cookie: 'io',
        ws: [Object],
        _events: [Object] } },
  adapter: 
   { nsp: 
      { name: '/',
        server: [Object],
        sockets: [Object],
        connected: [Object],
        fns: [],
        ids: 0,
        acks: {},
        adapter: [Circular],
        _events: [Object] },
     rooms: { '8uWFsXoTCSWbPJETAAAA': [Object] },
     sids: { '8uWFsXoTCSWbPJETAAAA': [Object] },
     encoder: {} },
  id: '8uWFsXoTCSWbPJETAAAA',
  client: 
   { server: 
      { nsps: [Object],
        _path: '/socket.io',
        _serveClient: true,
        _adapter: [Function: Adapter],
        _origins: '*:*',
        sockets: [Object],
        eio: [Object],
        httpServer: [Object],
        engine: [Object] },
     conn: 
      { id: '8uWFsXoTCSWbPJETAAAA',
        server: [Object],
        upgraded: false,
        readyState: 'open',
        writeBuffer: [Object],
        packetsFn: [Object],
        sentCallbackFn: [],
        request: [Object],
        remoteAddress: '192.168.11.98',
        checkIntervalTimer: null,
        upgradeTimeoutTimer: null,
        pingTimeoutTimer: [Object],
        transport: [Object],
        _events: [Object] },
     encoder: {},
     decoder: { reconstructor: null, _callbacks: [Object] },
     id: '8uWFsXoTCSWbPJETAAAA',
     request: 
      { _readableState: [Object],
        readable: false,
        domain: null,
        _events: {},
        _maxListeners: 10,
        socket: [Object],
        connection: [Object],
        httpVersion: '1.1',
        complete: false,
        headers: [Object],
        trailers: {},
        _pendings: [],
        _pendingIndex: 0,
        url: '/socket.io/?EIO=3&transport=polling&t=1417745564034-7',
        method: 'GET',
        statusCode: null,
        client: [Object],
        _consuming: true,
        _dumped: true,
        httpVersionMajor: 1,
        httpVersionMinor: 1,
        upgrade: false,
        _query: [Object],
        res: [Object],
        cleanup: [Function: cleanup],
        read: [Function] },
     onclose: [Function],
     ondata: [Function],
     onerror: [Function],
     ondecoded: [Function],
     sockets: [ [Circular] ],
     nsps: { '/': [Circular] },
     connectBuffer: [] },
  conn: 
   { id: '8uWFsXoTCSWbPJETAAAA',
     server: 
      { clients: [Object],
        clientsCount: 1,
        pingTimeout: 60000,
        pingInterval: 25000,
        upgradeTimeout: 10000,
        maxHttpBufferSize: 100000000,
        transports: [Object],
        allowUpgrades: true,
        allowRequest: [Function],
        cookie: 'io',
        ws: [Object],
        _events: [Object] },
     upgraded: false,
     readyState: 'open',
     writeBuffer: [ [Object] ],
     packetsFn: [ undefined ],
     sentCallbackFn: [],
     request: 
      { _readableState: [Object],
        readable: false,
        domain: null,
        _events: {},
        _maxListeners: 10,
        socket: [Object],
        connection: [Object],
        httpVersion: '1.1',
        complete: false,
        headers: [Object],
        trailers: {},
        _pendings: [],
        _pendingIndex: 0,
        url: '/socket.io/?EIO=3&transport=polling&t=1417745564034-7',
        method: 'GET',
        statusCode: null,
        client: [Object],
        _consuming: true,
        _dumped: true,
        httpVersionMajor: 1,
        httpVersionMinor: 1,
        upgrade: false,
        _query: [Object],
        res: [Object],
        cleanup: [Function: cleanup],
        read: [Function] },
     remoteAddress: '192.168.11.98',
     checkIntervalTimer: null,
     upgradeTimeoutTimer: null,
     pingTimeoutTimer: 
      { _idleTimeout: 85000,
        _idlePrev: [Object],
        _idleNext: [Object],
        _idleStart: 1417745564043,
        _onTimeout: [Function],
        _repeat: false },
     transport: 
      { readyState: 'opening',
        maxHttpBufferSize: 100000000,
        supportsBinary: true,
        _events: [Object],
        sid: '8uWFsXoTCSWbPJETAAAA',
        req: null,
        res: null,
        writable: false },
     _events: { close: [Object], data: [Function], error: [Function] } },
  rooms: [],
  acks: {},
  connected: true,
  disconnected: false,
  handshake: 
   { headers: 
      { host: 'n0bisuke.local:3000',
        connection: 'keep-alive',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36',
        accept: '*/*',
        referer: 'http://n0bisuke.local:3000/',
        'accept-encoding': 'gzip,deflate,sdch',
        'accept-language': 'ja,en;q=0.8,en-US;q=0.6,zh-TW;q=0.4,zh;q=0.2',
        cookie: 'io=PHZqa0gm4AyNJqUrAAAA; _ga=GA1.2.2023816835.1410404323' },
     time: 'Fri Dec 05 2014 11:12:44 GMT+0900 (JST)',
     address: '192.168.11.98',
     xdomain: false,
     secure: false,
     issued: 1417745564049,
     url: '/socket.io/?EIO=3&transport=polling&t=1417745564034-7',
     query: { EIO: '3', transport: 'polling', t: '1417745564034-7' } } }
```