itsource

발신인을 제외한 모든 클라이언트에 응답 보내기

mycopycode 2022. 9. 21. 00:03
반응형

발신인을 제외한 모든 클라이언트에 응답 보내기

모든 클라이언트에 송신하려면 , 다음의 순서를 사용합니다.

io.sockets.emit('response', data);

클라이언트로부터 수신하려면 , 다음을 사용합니다.

socket.on('cursor', function(data) {
  ...
});

클라이언트로부터 서버상의 메시지를 수신할 때 메시지를 송신하는 사용자를 제외한 모든 사용자에게 메시지를 송신하려면 어떻게 하면 좋을까요?

socket.on('cursor', function(data) {
  io.sockets.emit('response', data);
});

client-id를 메시지와 함께 보낸 후 client-side를 확인함으로써 해킹을 해야 하나요, 아니면 더 쉬운 방법이 없을까요?

다음은 내 목록입니다(1.0으로 업데이트됨).

// sending to sender-client only
socket.emit('message', "this is a test");

// sending to all clients, include sender
io.emit('message', "this is a test");

// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");

// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');

// sending to all clients in 'game' room(channel), include sender
io.in('game').emit('message', 'cool game');

// sending to sender client, only if they are in 'game' room(channel)
socket.to('game').emit('message', 'enjoy the game');

// sending to all clients in namespace 'myNamespace', include sender
io.of('myNamespace').emit('message', 'gg');

// sending to individual socketid
socket.broadcast.to(socketid).emit('message', 'for your eyes only');

// list socketid
for (var socketid in io.sockets.sockets) {}
 OR
Object.keys(io.sockets.sockets).forEach((socketid) => {});

@LearnRPG 답변에서 1.0:

 // send to current request socket client
 socket.emit('message', "this is a test");

 // sending to all clients, include sender
 io.sockets.emit('message', "this is a test"); //still works
 //or
 io.emit('message', 'this is a test');

 // sending to all clients except sender
 socket.broadcast.emit('message', "this is a test");

 // sending to all clients in 'game' room(channel) except sender
 socket.broadcast.to('game').emit('message', 'nice game');

 // sending to all clients in 'game' room(channel), include sender
 // docs says "simply use to or in when broadcasting or emitting"
 io.in('game').emit('message', 'cool game');

 // sending to individual socketid, socketid is like a room
 socket.broadcast.to(socketid).emit('message', 'for your eyes only');

@Crashalot 코멘트에 응답하려면socketid출처:

var io = require('socket.io')(server);
io.on('connection', function(socket) { console.log(socket.id); })

다음은 0.9.x에서 1.x로 변경된 사항에 대한 자세한 답변입니다.

 // send to current request socket client
 socket.emit('message', "this is a test");// Hasn't changed

 // sending to all clients, include sender
 io.sockets.emit('message', "this is a test"); // Old way, still compatible
 io.emit('message', 'this is a test');// New way, works only in 1.x

 // sending to all clients except sender
 socket.broadcast.emit('message', "this is a test");// Hasn't changed

 // sending to all clients in 'game' room(channel) except sender
 socket.broadcast.to('game').emit('message', 'nice game');// Hasn't changed

 // sending to all clients in 'game' room(channel), include sender
 io.sockets.in('game').emit('message', 'cool game');// Old way, DOES NOT WORK ANYMORE
 io.in('game').emit('message', 'cool game');// New way
 io.to('game').emit('message', 'cool game');// New way, "in" or "to" are the exact same: "And then simply use to or in (they are the same) when broadcasting or emitting:" from http://socket.io/docs/rooms-and-namespaces/

 // sending to individual socketid, socketid is like a room
 io.sockets.socket(socketid).emit('message', 'for your eyes only');// Old way, DOES NOT WORK ANYMORE
 socket.broadcast.to(socketid).emit('message', 'for your eyes only');// New way

@soyuka의 투고를 편집하고 싶었지만 피어 리뷰에 의해 편집이 거부되었습니다.

치트시트 내보내기

io.on('connect', onConnect);

function onConnect(socket){

  // sending to the client
  socket.emit('hello', 'can you hear me?', 1, 2, 'abc');

  // sending to all clients except sender
  socket.broadcast.emit('broadcast', 'hello friends!');

  // sending to all clients in 'game' room except sender
  socket.to('game').emit('nice game', "let's play a game");

  // sending to all clients in 'game1' and/or in 'game2' room, except sender
  socket.to('game1').to('game2').emit('nice game', "let's play a game (too)");

  // sending to all clients in 'game' room, including sender
  io.in('game').emit('big-announcement', 'the game will start soon');

  // sending to all clients in namespace 'myNamespace', including sender
  io.of('myNamespace').emit('bigger-announcement', 'the tournament will start soon');

  // sending to individual socketid (private message)
  socket.to(<socketid>).emit('hey', 'I just met you');

  // sending with acknowledgement
  socket.emit('question', 'do you think so?', function (answer) {});

  // sending without compression
  socket.compress(false).emit('uncompressed', "that's rough");

  // sending a message that might be dropped if the client is not ready to receive messages
  socket.volatile.emit('maybe', 'do you really need it?');

  // sending to all clients on this node (when using multiple nodes)
  io.local.emit('hi', 'my lovely babies');

};

broadcast.messages는 다른 모든 클라이언트에 메시지를 전송합니다(송신인 제외).

socket.on('cursor', function(data) {
  socket.broadcast.emit('msg', data);
});

룸 내 네임스페이스에 대해 룸 내 클라이언트 목록을 루프하는 것(Nav의 답변과 유사)은 두 가지 접근법 중 하나라고 생각합니다.다른 하나는 제외를 사용하는 것입니다.예.

socket.on('message',function(data) {
    io.of( 'namespace' ).in( data.roomID ).except( socket.id ).emit('message',data);
}

V4.x의 서버측 유효한 모든 송신 이벤트 리스트

io.on("connection", (socket) => {

// basic emit
socket.emit(/* ... */);

// to all clients in the current namespace except the sender
socket.broadcast.emit(/* ... */);

// to all clients in room1 except the sender
socket.to("room1").emit(/* ... */);

// to all clients in room1 and/or room2 except the sender
socket.to(["room1", "room2"]).emit(/* ... */);

// to all clients in room1
io.in("room1").emit(/* ... */);

// to all clients in room1 and/or room2 except those in room3
io.to(["room1", "room2"]).except("room3").emit(/* ... */);

// to all clients in namespace "myNamespace"
io.of("myNamespace").emit(/* ... */);

// to all clients in room1 in namespace "myNamespace"
io.of("myNamespace").to("room1").emit(/* ... */);

// to individual socketid (private message)
io.to(socketId).emit(/* ... */);

// to all clients on this node (when using multiple nodes)
io.local.emit(/* ... */);

// to all connected clients
io.emit(/* ... */);

// WARNING: `socket.to(socket.id).emit()` will NOT work, as it will send to everyone in the room
// named `socket.id` but the sender. Please use the classic `socket.emit()` instead.

// with acknowledgement
socket.emit("question", (answer) => {
    // ...
});

// without compression
socket.compress(false).emit(/* ... */);

// a message that might be dropped if the low-level transport is not writable
socket.volatile.emit(/* ... */);

});

클라이언트측

// basic emit
    socket.emit(/* ... */);

// with acknowledgement
    socket.emit("question", (answer) => {
        // ...
    });

// without compression
    socket.compress(false).emit(/* ... */);

// a message that might be dropped if the low-level transport is not writable
    socket.volatile.emit(/* ... */);

소스 = > http://filename.today/http.06.22-023900/https://socket.io/docs/v4/emit-cheatsheet/index.html 에 대한 아카이브 링크

기타 케이스

io.of('/chat').on('connection', function (socket) {
    //sending to all clients in 'room' and you
    io.of('/chat').in('room').emit('message', "data");
};

추가 문서화를 위해 목록을 업데이트.

socket.emit('message', "this is a test"); //sending to sender-client only
socket.broadcast.emit('message', "this is a test"); //sending to all clients except sender
socket.broadcast.to('game').emit('message', 'nice game'); //sending to all clients in 'game' room(channel) except sender
socket.to('game').emit('message', 'enjoy the game'); //sending to sender client, only if they are in 'game' room(channel)
socket.broadcast.to(socketid).emit('message', 'for your eyes only'); //sending to individual socketid
io.emit('message', "this is a test"); //sending to all clients, include sender
io.in('game').emit('message', 'cool game'); //sending to all clients in 'game' room(channel), include sender
io.of('myNamespace').emit('message', 'gg'); //sending to all clients in namespace 'myNamespace', include sender
socket.emit(); //send to all connected clients
socket.broadcast.emit(); //send to all connected clients except the one that sent the message
socket.on(); //event listener, can be called on client to execute on server
io.sockets.socket(); //for emiting to specific clients
io.sockets.emit(); //send to all connected clients (same as socket.emit)
io.sockets.on() ; //initial connection from a client.

이게 도움이 됐으면 좋겠다.

Socket.io v4.0.0+용으로 갱신되었습니다.

v4에서 보낸 사람을 제외한 모든 클라이언트에 내보내려면 다음을 사용합니다.

socket.broadcast.emit(/* ... */);

사용 예

io.on("connection", (socket) => {
   socket.on("message", (message) => {
      //sends "newMessage" to all sockets except sender
      socket.broadcast.emit("newMessage", message)
   })
})

이 코드 사용

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

    socket.on('mousemove', function (data) {

        socket.broadcast.emit('moving', data);
    });

이 socket.socket.socket()은, 송신하고 있는 서버를 제외하고, 함수의 모든 것을 송신합니다.

네임스페이스와 방을 사용하고 있습니다.

socket.broadcast.to('room1').emit('event', 'hi');

어디서 일하다

namespace.broadcast.to('room1').emit('event', 'hi');

하지 않았다

(다른 누군가가 그 문제에 직면했을 경우)

언급URL : https://stackoverflow.com/questions/10058226/send-response-to-all-clients-except-sender

반응형