2014-09-26 43 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