Why is the broadcast described as flag in the docs when it is actually an object?

自作多情 提交于 2019-12-25 16:24:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!