在客户端销毁Socket.io

时间:2015-01-27 08:23:19

标签: javascript socket.io mootools

早上好, 我有一个客户端Mootools课程。在这个类里面我有一个socket.io变量。 从服务器端我发送服务消息(发送)到客户端,我希望客户端断开连接,重新启动socket.io并重新连接到服务器。 我的问题在于reinizialize socket.io变量。 我尝试命令“delete socket.io”但似乎不起作用,当socket.io重新连接到服务器时,它会收到两次数据消息。

如何删除类中的socket.io变量并重新创建? 提前致谢

var Push = new Class({

Implements: [Events],

attachSocketIOEvents : function() {
    var self = this;

    self.socketIO.on('connecting', function () {
        self.logger.info('Connessione in corso...');
    });

    self.socketIO.on('connect', function(){
        self.clientId = self.socketIO.io.engine.id;
        self.logger.info('Connessione avvenuta. Id associato: ', self.socketIO.io.engine.id);
    });

    self.socketIO.on('connect_failed', function() {
        self.logger.error('Connessione fallita');
        self.initSocketIOConnection();
    });

    self.socketIO.on('reconnecting', function() {
        self.logger.error('Riconnessione in corso');
        self.isReconnect = true;
    });

    self.socketIO.on('disconnect', function() {
        self.logger.error('Client disconnesso');
    });

    self.socketIO.on('error', function (error) {
        self.logger.error(error);
    });

    self.socketIO.on('servicedata', function(data) {
        self.processServiceMessage(data);
    });

    self.socketIO.on('updatedata', function(data) {
        self.processUpdateMessage(data);
    });
},

initialize : function(domContainer) {
    var self = this;

    this.socketIO = '';
    this.clientId = '';
    this.container = '';

    this.socketIoUrl = ''.concat(window.location.protocol, '//', window.location.host, '/mfwebpushjs');


    if (this.isValidHTML) {
        self.processDOMButtons();
        self.attachPushClassEvents();
    }
},


initSocketIOConnection: function() {
    var self = this;

    if (self.socketIO === '' || self.socketIO.connected == false) {
        self.socketIO = io.connect(this.socketIoUrl, {
            'query': "enviroment=" + self.enviroment + "&userName=" + self.userName,
            'connect timeout': 500,
            'reconnect': true,
            'reconnection delay': 0,
            'reopen delay': 500,
            'max reconnection attempts': 0,
            'forceNew': true
        });

        self.logger.debug ("Socket io connesso verso l\'url ", this.socketIoUrl);
        self.attachSocketIOEvents();
    }

    // Creato socket.io room
    self.socketIO.emit('create_room', this.filterName);
},


processServiceMessage: function(message) {
    var self = this;
    var messageSplitted = message.split("|");       
    if (messageSplitted[0] === 'ConnectedToNodeJS') {
        // doSomething
    } else if (messageSplitted[0] === 'ConsoleLog') {
        // doSomething
    } else if (messageSplitted[0] === 'DisconnectForInstanceLock') {
        // doSomething
    } else  if (messageSplitted[0] === 'ForceReconnect') {
        if (this.filterName == messageSplitted[1]) {
            self.unsubscribe();
            self.socketIO.disconnect();             
            delete window.socketIO;
            self.initSocketIOConnection();
            self.subscribe();
        }           
    } else if (messageSplitted[0] === 'DisconnectForAkkaDown') {
        // doSomething      
    } else if (messageSplitted[0] === 'ReconnectAfterAkkaDown') {
        t// doSomething
    }  else if (messageSplitted[0] === 'ConnectedToAkka') {
        // doSomething
    } else {
        this.logger.info(message);
    }
},

});

0 个答案:

没有答案