All files / plugins/socket index.js

29.41% Statements 5/17
0% Branches 0/4
0% Functions 0/6
29.41% Lines 5/17
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  1x 1x   1x   1x   1x                                                              
// Create a socket plugin
const socketIO = require('socket.io');
const extend = require('extend');
 
const listeners = {};
 
const router = require('../../router');
 
module.exports = {
  name: 'socket',
  options: {},
  router: (...all) => {
    const { path, middle } = router.parse(all);
    console.log(path, middle);
    // const [ path, middle ] = all;
    // listeners[path] = listeners[path] || [];
    // listeners[path].push(middle);
  },
  launch: ctx => {
    ctx.io = socketIO(ctx.server);
    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 }));
        });
      }
    });
  }
};