简体   繁体   中英

node.js socket.io middleware

In node.js we can add some middleware function to socket.io server like this

 io.use(function(socket, next) {
            var req = socket.handshake;
            var res = {};
            cookieParser(req, res, function(err) {
                if (err) return next(err);
                session(req, res, next);
            });
        });

I wonder if we can add some kind of middleware to simple socket.on function as in the following example

io.sockets.on('connection', function(socket){
    socket.on('someSignal',
             function(data,next){/*middleware function code*/ next();},
             function(data){})
});

emitter.on(event, listener) is the alias for emitter.addListener(event, listener). if you want to do this, every request pass through this middleware will add one eventListener.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM