如何将socket.io与mootools集成

时间:2014-09-26 09:22:11

标签: javascript socket.io mootools

我有一个关于mootools和socket.io在客户端上的集成的问题:

假设: - 在node.js中开发的服务器应用程序,它有一个socket.io listen

我想定义一个类来管理与服务器的连接,客户端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
    }

});

2 个答案:

答案 0 :(得分:4)

您的this班级实例似乎已失去Push个上下文。

要解决此问题,您需要修改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;
}

答案 1 :(得分:1)

现在可行。

Socket.io init必须在initialize调用的特定方法中定义。 nitialize中的直接初始化不起作用:

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();
    }
},

感谢大家。