如何将socket.io与mootools集成

How to integrate socket.io with mootools

本文关键字:mootools 集成 io socket      更新时间:2023-09-26

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

假设:-在node.js中开发的服务器应用程序,该应用程序具有socket.io侦听

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

您似乎丢失了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;
}

现在它可以工作了。

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

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

感谢大家。