'use strict';

import { log, LOG_TYPES } from './log';

/*
 * Socket.io related things go !
 */

let io;

let connections = [];

function createSocketIO(server, app) {
    io = require('socket.io')(server);
 
    io.on('connection', (socket) => {
        log(`Socket connected with id ${socket.id}`);
        connections.push(socket);

        socket.on('disconnect', () => {
            log(`Socket disconnected with id ${socket.id}`, LOG_TYPES.WARN);

            var i = connections.indexOf(socket);
            connections.splice(i, 1);
        });

    });
}


export { io, connections, createSocketIO }


@@ -48,17 +48,62 @@
 ./log';%0A
+import decodeJWT          from './decodeJWT';
 %0A
-
 /*%0A * So
@@ -249,10 +249,9 @@
 r);%0A
- 
 %0A
+
     
@@ -287,96 +287,8 @@
 =%3E %7B
-%0A        log(%60Socket connected with id $%7Bsocket.id%7D%60);%0A        connections.push(socket);
 %0A%0A  
@@ -499,24 +499,448 @@
        %7D);%0A%0A
+        decodeJWT(socket.handshake.query.token)%0A            .then( results =%3E %7B%0A                log(%60Socket connected with id $%7Bsocket.id%7D%60);%0A                socket.user = results;%0A                connections.push(socket);%0A            %7D)%0A            .catch(error =%3E %7B%0A                console.log('token is invalid');%0A                socket.emit('token_invalid', %7B%7D); %0A                socket.disconnect(true);%0A            %7D)%0A
     %7D);%0A%7D%0A%0A%0A

Check JWT token on socket connection