2014-09-26 40 views
1

我有一個關於mootools的之間的集成問題,socket.io客戶端socket.io:如何整合與MooTools的

假設: - 其中有一個socket.io的node.js開發的服務器應用程序監聽

我想定義一個類來管理與服務器的連接,客戶端socket.io必須駐留在這個類中。

其實我能夠從這個類發送連接,但我無法管理推事件。如何更正此代碼?

var Push = new Class({ 

    Implements: [Events], 
    initialize : function() { 
     this.socketServer = '192.168.1.3'; 
     this.listeningPort = '8080'; 
     this.socketIoUrl = 'http://'.concat(this.socketServer,':', this.listeningPort); 

     // 
     this.socketIO = io.connect(this.socketIoUrl, { 
      'connect timeout' : 500, 
      'reconnect' : false, 
      'reconnection delay' : 0, 
      'reopen delay' : 500, 
      'max reconnection attempts' : 0 
     }); 

     // Attach Socket.io events 
     this.attachEvents(); 

     // Creating a socket.io room 
     this.socketIO.emit('create', this.filterName); 
    }, 

    // SOCKET.IO EVENTS 
    attachEvents : function() { 
     socketIO.on = function(e) { 
      log.info('aaa'); 
      socket.on('disconnect', function() { 
       log.error("SOCKET.IO CLIENT disconnected"); 
       this.fireEvent("disconnect", [ e.data, e ]); 
      }); 

      socket.on('connect_failed', function() { 
       log.error("SOCKET.IO connection failed "); 
       this.fireEvent("connect_failed", [ e.data, e ]); 
      }); 

      socket.on('message', function() { 
       log.debug(e.data); 
       processMessage(e.data); 
       this.fireEvent("message", [ e.data, e ]); 
      }); 

     }.bind(this) 

     return this 
    } 

}); 

回答

1

現在,它的工作原理。

Socket.io init必須在由initialize調用的特定方法中定義。在nitialize直接initializatio不起作用:

initialize : function(filterName, instrumentCode, fieldList, bankName, userName) { 
    var self = this; 

    .... 
    self.initConnection(); 
    self.attachEvents(); 
}, 

initConnection : function() { 
    var self = this; 

    self.socketIO = io.connect(this.socketIoUrl, { 
     'connect timeout' : 500, 
     'reconnect' : true, 
     'reconnection delay' : 0, 
     'reopen delay' : 500, 
     'max reconnection attempts' : 0 
    }); 

    logger.debug ('socket.io init'); 
}, 

attachEvents : function() { 
    var self = this; 

    // Attach Socket.io events 
    //this.attachEvents(); 
    self.socketIO.on('disconnect', function() { 
     logger.error('Client disconnected'); 
     self.initConnection(); 
     self.resendRequest(); 
    }); 

    self.socketIO.on('connect_failed', function() { 
     logger.error('Connection failed'); 
     self.initConnection(); 
     self.resendRequest(); 
    }); 

    self.socketIO.on('message', function(data) { 
     self.processMessage(data); 
    }); 

    self.socketIO.emit('create', this.filterName); 
}, 

resendRequest : function() { 
    if (this.operationType == "SUBSCRIBE") { 
     subscribe(); 
    } else { 
     unsubscribe(); 
    } 
}, 

感謝大家。

+4

看來,你的問題不是由socket.io或mootools引起的... – Inferpse 2014-09-29 15:41:13

+0

另一次我會多加註意帶來很多元素,謝謝。 – 2014-09-29 19:45:18

4

看來,你已經失去了你Push類實例的this上下文。

爲了解決這個問題,你需要修改attachEvents功能是這樣的:

// SOCKET.IO EVENTS 
attachEvents : function() { 
    var self = this; // save context to variable called "self" 

    this.socketIO.on('disconnect', function() { 
     log.error("SOCKET.IO CLIENT disconnected"); 
     self.fireEvent("disconnect", [ e.data, e ]); 
    }); 

    this.socketIO.on('connect_failed', function() { 
     log.error("SOCKET.IO connection failed "); 
     self.fireEvent("connect_failed", [ e.data, e ]); 
    }); 

    this.socketIO.on('message', function() { 
     log.debug(e.data); 
     processMessage(e.data); 
     self.fireEvent("message", [ e.data, e ]); 
    }); 

    return this; 
} 
+0

即使做了'var f = this.fireEvent; ... f('message',e.data,e)'等等,以避免查找屬性,因爲您沒有引用任何其他 – 2014-09-27 01:55:30

+0

我發現了另一個問題:io.connect(不能直接調用,否則初始化,連接 – 2014-09-29 09:26:56

+1

我認爲socket.io可以隨時初始化,爲什麼不能在'Push'類的構造函數中調用它? – Inferpse 2014-09-29 11:16:07