socket.broadcast.emit()
์ฌ์ฉํ์ฌ ๋ค๋ฅธ ํด๋ผ์ด์ธํธ์๊ฒ ๋ธ๋ก๋์บ์คํธํ๋ ๋์ ์ด ์ด์ํ ๋์์ ๋ฐ๊ฒฌํ์ต๋๋ค.
๋ฌธ์ ์ ์ํ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
...notifyOtherClients = function(room, e, data, socket)
{
// sending to all clients in a room except sender
var transport = socket.broadcast.to(room);
//
setTimeout(function(){
// #method1: this works as expected: all clients receive event except the sender
socket.broadcast.to(room).emit(e, data);
// #method2: this doesn't work though: all clients receive this event. Why?! Bug?
transport.emit(e, data);
// logs out 'true'
console.log(transport === socket.broadcast.to(room));
},
1000);
};
...
notifyOtherClients("myRoom", "someEvent", { }, socket);
๋ด๊ฐ ์ฌ๊ธฐ์ ๋ฌด์์ ๋์น๊ณ ์์ต๋๊น? #method2๊ฐ ์์๋๋ก ์๋ํ์ง ์๋ ์ด์ .
๊ฐ์ฌ ํด์.
socket.broadcast.to(room)
์ ์์ต์ ๋ฌด์์
๋๊น?
@Mad-Head ๋ฌธ์์ ๋ฐ๋ฅด๋ฉด ์ฒด์ธ ๊ฐ๋ฅ์ฑ์ ์ ์งํ๊ธฐ ์ํด ๋ค๋ฅธ ๋ชจ๋ ๋ฐฉ๋ฒ๊ณผ ๊ฑฐ์ ๋์ผํ ์์ผ ์ธ์คํด์ค๋ฅผ ๋ฐํํฉ๋๋ค.
http://socket.io/docs/server-api/#socket #to(๋ฐฉ:๋ฌธ์์ด):์์ผ
์ฌ๋ฏธ์๋ ๊ฒ์ ์ด๊ฒ์ด ์ฌ์ค์์ ์ถ์ ํ๋ค๋ ๊ฒ์ ๋๋ค.
console.log(transport === socket.broadcast.to(room));
์๋
ํ์ธ์! socket.broadcast
์ค์ ๋ก ์์ ํ๋๊ทธ๋ฅผ ์ถ๊ฐ (์ฆ, broadcast
) ๋ค์์ ์ฌ์ฉ๋๋ emit
์จ์ด๋ผ๊ณ socket
.
ํ๋๊ทธ๋ ์ฒ์ ์ฌ์ฉ๋ ๋ค์ ์ญ์ ๋๋ฏ๋ก ๋ ๋ฒ์งธ ํธ์ถ์ ๊ฒฐ๊ณผ์ ๋๋ค.
์๋ ํ์ธ์! ํ ... ์ ๊ฐ ์ถฉ๋ถํ ์ดํดํ์ง ๋ชปํ์ ์๋ ์์ต๋๋ค. ์ข ๋ ๋ช ํํ ํ์๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
์ํ1
setTimeout(function(){
// #method1: this works as expected: all clients receive event except the sender
socket.broadcast.to(room).emit(e, data);
},
1000);
์ํ2
var transport = socket.broadcast.to(room);
setTimeout(function(){
// #method2: this doesn't work though: all clients receive this event. Why?! Bug?
transport.emit(e, data);
},
1000);
๋ฐ๋ผ์ ํ๋๊ทธ์ ๊ด๋ จ์ด ์์ต๋๋ค. ์์ ๋ ์ํ์ ๋์ผํ๊ฒ ์๋ํด์ผ ํ์ง๋ง ์๋ํ์ง ์์ต๋๋ค. ๊ทธ๊ฒ์ด ๋ฌธ์ ์ด๊ณ ๋๋ ๊ทธ ์ด์ ๋ฅผ ์์๋ด๊ธฐ ์ํด ํ๋ฃจ์ 1/2 ์ ๋ ๋ฒฝ์ ๋๋๋ฆฌ๊ณ ์์ต๋๋ค.
UPD: ์ค์ ๋ก @darrachequesne ๋น์ ๋ง์ด ๋ง์ต๋๋ค. ๋๋ ์์ค๋ฅผ ์กฐ์ฌํ๋ค. ๋งค๋ฒ emit()์ด ํธ์ถ๋ ๋๋ง๋ค _rooms๊ฐ ํ๋ฌ์๋๊ณ setTimeout์ด ์์ง ์งํ ์ค์ธ ๋์์ emit()์ด๋ผ๋ ๋ค๋ฅธ ๋ฉ์๋๊ฐ ์งํ๋๊ณ ๋ฐฉ์ ํ๋ฌ์ํ๋ ๋์ ์ด์ํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค(ping์ด ๋ฌธ์ ์์ต๋๋ค)
๊ทธ๊ฒ ๋ฌด์จ ์ผ์ด์ผ
1. setup a room with to()
2. start timeout for 1000 ms
...
[some where between 0 and 1000 ms] ping occurs i.e. emit() and flushes the room
...
3. timeout calls socket but the room's already flushed.
4. *problemo* all clients get notified including the sender socket.
@darrachequesne ์ ์์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
์ด๊ฒ์ ๋ซ๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์๋ ํ์ธ์!
socket.broadcast
์ค์ ๋ก ์์ ํ๋๊ทธ๋ฅผ ์ถ๊ฐ (์ฆ,broadcast
) ๋ค์์ ์ฌ์ฉ๋๋emit
์จ์ด๋ผ๊ณsocket
.ํ๋๊ทธ๋ ์ฒ์ ์ฌ์ฉ๋ ๋ค์ ์ญ์ ๋๋ฏ๋ก ๋ ๋ฒ์งธ ํธ์ถ์ ๊ฒฐ๊ณผ์ ๋๋ค.