Programing

broadcast.to와 sockets.in의 Socket.io 방 차이점

lottogame 2020. 8. 20. 19:23
반응형

broadcast.to와 sockets.in의 Socket.io 방 차이점


Socket.io의 readme에는 다음 예제가 포함되어 있습니다.

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.join('justin bieber fans');
  socket.broadcast.to('justin bieber fans').emit('new fan');
  io.sockets.in('rammstein fans').emit('new non-fan');
});

socket.broadcast.to()의 차이점은 무엇입니까 io.sockets.in()?


socket.broadcast.to지정된 룸의 모든 소켓으로 브로드 캐스트하는 동안 호출 된 소켓을 제외하고 지정된 룸의 io.sockets.in모든 소켓으로 브로드 캐스트합니다.


Node.js는 제가 한동안 정말 흥미로 웠던 것이었고, 멀티 플레이어 게임을 만들기 위해 제 프로젝트 중 하나에서 사용했습니다.

io.sockets.in().emit()그리고 socket.broadcast.to().emit()Socket.io의 방 ( https://github.com/LearnBoost/socket.io/wiki/Rooms ) 에서 사용하는 두 가지 주요 방출 방법입니다. 방은 연결된 클라이언트의 간단한 분할을 허용합니다. 이렇게하면 연결된 클라이언트 목록의 하위 집합으로 이벤트를 내보낼 수 있으며이를 관리하는 간단한 방법이 제공됩니다.

이를 통해 연결된 클라이언트 목록 (우리가 룸이라고 부름)의 하위 집합을 관리 할 수 ​​있으며 주요 socket.io 함수 io.sockets.emit()socket.broadcast.emit().

어쨌든 설명 할 주석과 함께 예제 코드를 제공하려고합니다. 도움이되는지 확인하십시오.

Socket.io 룸

i) io.sockets.in (). emit ();

/* Send message to the room1. It broadcasts the data to all 
   the socket clients which are connected to the room1 */

io.sockets.in('room1').emit('function', {foo:bar});

ii) socket.broadcast.to (). emit ();

io.sockets.on('connection', function (socket) {
    socket.on('function', function(data){

        /* Broadcast to room1 except the sender. In other word, 
            It broadcast all the socket clients which are connected 
            to the room1 except the sender */
        socket.broadcast.to('room1').emit('function', {foo:bar});

    }
}

Socket.io

i) io.sockets.emit ();

/* Send message to all. It broadcasts the data to all 
   the socket clients which are connected to the server; */

io.sockets.emit('function', {foo:bar});

ii) socket.broadcast.emit ();

io.sockets.on('connection', function (socket) {
    socket.on('function', function(data){

        // Broadcast to all the socket clients except the sender
        socket.broadcast.emit('function', {foo:bar}); 

    }
}

건배


업데이트 2019 : socket.io는 웹 소켓을 사용하고 http 요청 폴링으로 대체하는 특수 모듈입니다. 웹 소켓의 경우 : 클라이언트의 경우 네이티브 웹 소켓을 사용하고 node.js의 경우 ws 또는이 라이브러리를 사용합니다.

간단한 예

The syntax is confusing in socketio. Also, every socket is automatically connected to their own room with the id socket.id (this is how private chat works in socketio, they use rooms).

Send to the sender and noone else

socket.emit('hello', msg);

Send to everyone including the sender(if the sender is in the room) in the room "my room"

io.to('my room').emit('hello', msg);

Send to everyone except the sender(if the sender is in the room) in the room "my room"

socket.broadcast.to('my room').emit('hello', msg);

Send to everyone in every room, including the sender

io.emit('hello', msg); // short version

io.sockets.emit('hello', msg);

Send to specific socket only (private chat)

socket.broadcast.to(otherSocket.id).emit('hello', msg);

In Socket.IO 1.0, .to() and .in() are the same. And others in the room will receive the message. The client sends it won't receive the message.

Check out source code (v1.0.6):

https://github.com/Automattic/socket.io/blob/a40068b5f328fe50a2cd1e54c681be792d89a595/lib/socket.js#L173

참고URL : https://stackoverflow.com/questions/6873607/socket-io-rooms-difference-between-broadcast-to-and-sockets-in

반응형