问题
In the broadcasting messages section http://socket.io/docs/#broadcasting-messages there is the following description
To broadcast, simply add a broadcast flag to emit and send method calls. Broadcasting means sending a message to everyone else except for the socket that starts it. Server
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.broadcast.emit('user connected');
});
The broadcast is an object. Why does the doc description refers it as flag
回答1:
Internally in the socket.io implementation, broadcast
is a flag that is sent with an emit that tells the underlying infrastructure what to do. See the source for that flag here and you can see here in the source where it tests for that flag on a socket to decide if a given emit
should be broadcast.
The broadcast
in socket.broadcast.emit()
is indeed an object.
broadcast
is also a method on the adapter object which is used internally by socket.io. So, they've used the term for just about everything (flag, object, method). The doc has a few of the different uses confused in the case you reference.
The socket.io doc is what it is (not nearly as good as it could be). I find constant references to the source code on Github or even tracing into the execution in a debugger to be essential for understanding how things work.
来源:https://stackoverflow.com/questions/28519114/why-is-the-broadcast-described-as-flag-in-the-docs-when-it-is-actually-an-object