简体   繁体   中英

how does event emitter pattern work in Node , sockets.io?

I am trying to get my hands on node ,reading through the book node.js in action ,I am came across this bit of code which is to be written on server side.

var socketio = require('socket.io');
socket.on('rooms', function() {
socket.emit('rooms', io.sockets.manager.rooms);
});

I am not sure how this works. As far as I understood the method socket.on() registers/adds an event listener to listen to events of type rooms and then callbacks a function which emits a rooms event..?? is that how it works? its not making sense... when does the socket object emits rooms event?

This is a custom event, triggered by the other side. In this server side example, server waits for the client sends rooms event, and in the callback it responds with rooms event as well. Client in turn can also listen for rooms event.

You could name these events as you want. It is not necessary that both these events are named rooms .

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