All files / server-old/plugins/socket index.js

38.89% Statements 7/18
12.5% Branches 1/8
16.67% Functions 1/6
41.18% Lines 7/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57  26x 26x   26x   26x                                                         106x 106x 106x                                      
// Create a socket plugin
const socketIO = require('socket.io');
const extend = require('extend');
 
const listeners = {};
 
module.exports = {
  name: 'socket',
  options: {
    path: {
      env: 'SOCKET_PATH'
    },
    serveClient: {},
    adapter: {},
    origins: {},
    parser: {},
    pingTimeout: {},
    pingInterval: {},
    upgradeTimeout: {},
    maxHttpBufferSize: {},
    allowRequest: {},
    transports: {},
    allowUpgrades: {},
    perMessageDeflate: {},
    httpCompression: {},
    cookie: {},
    cookiePath: {},
    cookieHttpOnly: {},
    wsEngine: {}
  },
  router: (path, middle) => {
    listeners[path] = listeners[path] || [];
    listeners[path].push(middle);
  },
  launch: ctx => {
    Iif (!ctx.options.socket) return;
    ctx.io = socketIO(ctx.server, ctx.options.socket);
    ctx.io.on('connect', socket => {
      // console.log(socket.client.request.session);
      for (let path in listeners) {
        if (path !== 'connect') {
          listeners[path].forEach(cb => {
            socket.on(path, data => {
              cb(extend(socket.client.request, ctx, { path, socket, data }));
            });
          });
        }
      }
      if (listeners['connect']) {
        listeners['connect'].forEach(cb => {
          cb(extend(socket.client.request, ctx, { path: 'connect', socket }));
        });
      }
    });
  }
};