SocketServer.prototype.init = function()

in src/server/socket.js [45:121]


SocketServer.prototype.init = function (server) {
    var that = this,
        config = this._simulatorProxy.config;

    this._io = require('socket.io')(server);

    this._io.on('connection', function (socket) {
        // Debug messages can be sent to the external debug-host before the app and the simulator are fully ready (for
        // example, during a plugin initialization). For that reason, the debug-message handler should be subscribed to
        // immediately, regardless of which socket type has just connected.
        socket.on('debug-message', function (data) {
            that._emitTo(DEBUG_HOST, data.message, data.data);
        });

        socket.on('register-app-host', function () {
            log.log('APP_HOST connected to the server');
            if (that._hostSockets[APP_HOST]) {
                log.log('Overriding previously connected APP_HOST');
                that._resetAppHostState();
            }
            that._hostSockets[APP_HOST] = socket;
            that._whenSimHostReady.promise
                .then(that._onSimHostReady.bind(that));
            that._whenAppHostConnected.resolve();
        });

        socket.on('register-simulation-host', function () {
            log.log('SIM_HOST connected to the server');
            if (that._hostSockets[SIM_HOST]) {
                log.log('Overriding previously connected SIM_HOST');
                that._resetSimHostState();
            }
            that._hostSockets[SIM_HOST] = socket;
            that._handleSimHostRegistration(socket);
        });

        socket.on('register-debug-host', function (data) {
            log.log('DEBUG_HOST registered with server.');

            // It only makes sense to have one debug host per server. If more than one tries to connect, always take
            // the most recent.
            that._hostSockets[DEBUG_HOST] = socket;

            if (data && data.handlers) {
                config.debugHostHandlers = data.handlers;
            }

            that._handlePendingEmits(DEBUG_HOST);
        });

        socket.on('disconnect', function () {
            var type;
            Object.keys(that._hostSockets).forEach(function (t) {
                if (that._hostSockets[t] === socket) {
                    type = t;
                }
            });
            if (!type) {
                log.log('Disconnect for an inactive socket');
                return;
            }
            that._hostSockets[type] = undefined;
            log.log(type + ' disconnected from the server');
            switch (type) {
                case APP_HOST:
                    that._resetAppHostState();
                    break;
                case SIM_HOST:
                    that._resetSimHostState();
                    break;
                case DEBUG_HOST:
                    config.debugHostHandlers = null;
                    break;
            }
        });
    });
};