简体   繁体   中英

What's difference between io.emit(…) and loop?

First of all, I hope you understand my poor english skill.

I'm developing a server using socket.io and server contains all client's 'socket' variable that given as parameter to 'connection'. and I want to broadcast 'message' without all functions of 'io' object. so I plan to implement broadcast function like that.

let socketList = [ ... ]; // <-- that array contains socket of client.
for (let i = 0; i < socketList.length; ++i)
    socketList[i].emit(...);

but It makes no sence to me. because I don't know that's same with io.emit function. so my question is: Is that code same with io.emit(...)?

That code and io.emit() will do the same thing if all your clients really are in socketList . If this is about performance and you don't have to do anything fancy like filtering out certain clients, I would go with io.emit() , that way you won't need to keep your own socketList.

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