Source: socket_handler.js

/** 
 * This file handles all Socket Requests to the Server.
 * 
 * @module scripts/socket_handler
 */

/**
 * Class for handling all Socket Requests.
 * 
 * @class SocketHandler
 */
class SocketHandler {

    /**
     * Constructs a new Socket Handler.
     * 
     * @constructor
     */
    constructor(requestHandler, pageHandler) {

        this.socket = null;

        /**
         * Creates a socket connection with the server and sets up events.
         * 
         * @function createConnection
         */
        this.createConnection = () => {

            let baseURL = document.URL;

            const port = 5000;
            let portSeperator = ":";

            let urlFigures = baseURL.split(portSeperator);
            let urlProtocol = urlFigures[0];
            let socketURL = urlFigures[1];

            let connectionURL = urlProtocol + portSeperator 
                                + socketURL + portSeperator + port.toString();

            this.socket = io(connectionURL, {

                auth: { token: requestHandler.getCookieValue('token') }

            });

            this.socket.on('update', (data) => {

                pageHandler.updateConsole(data);

            });

        }

    }

}

export default SocketHandler;