252

すべてのクライアントに何かを送信するには、次を使用します。

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

クライアントから受信するには、次を使用します。

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

サーバー上でクライアントからメッセージを受信するときに、メッセージを送信するユーザーを除くすべてのユーザーにそのメッセージを送信するように、2つを組み合わせるにはどうすればよいですか?

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

メッセージとともにclient-idを送信し、クライアント側で確認することでハックする必要がありますか、それとももっと簡単な方法がありますか?

4

12 に答える 12

927

これが私のリストです(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) => {});
于 2012-04-11T03:15:25.450 に答える
44

@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); })
于 2015-01-17T18:23:32.543 に答える
13

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の投稿を編集したかったのですが、査読により編集が却下されました。

于 2015-06-06T12:00:10.437 に答える
8

虎の巻を発行する

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');

};
于 2018-01-31T05:57:40.670 に答える
7

broadcast.emitは、他のすべてのクライアント(送信者を除く)にメッセージを送信します

socket.on('cursor', function(data) {
  socket.broadcast.emit('msg', data);
});
于 2012-04-08T01:05:08.190 に答える
4

部屋内の名前空間の場合、部屋内のクライアントのリストをループすること(Navの回答と同様)は、私が見つけた2つのアプローチのうちの1つです。もう1つは、excludeを使用することです。例えば

socket.on('message',function(data) {
    io.of( 'namespace' ).in( data.roomID ).except( socket.id ).emit('message',data);
}
于 2013-02-05T17:33:30.303 に答える
1

その他の場合

io.of('/chat').on('connection', function (socket) {
    //sending to all clients in 'room' and you
    io.of('/chat').in('room').emit('message', "data");
};
于 2015-01-19T15:49:32.257 に答える
1

詳細なドキュメントのためにリストを更新しました。

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.

お役に立てれば。

于 2016-11-27T14:13:43.367 に答える
1

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://archive.today/2021.06.22-023900/https://socket.io/docs/v4/emit-cheatsheet/index.html

于 2021-06-22T02:42:35.823 に答える
1

Socket.iov4.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)
   })
})
于 2021-10-11T13:24:52.133 に答える
0

このコーディングを使用する

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

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

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

このsocket.broadcast.emit()は、発行しているサーバーを除いて、関数内のすべてのものを発行します

于 2013-01-15T11:08:52.907 に答える
0

名前空間と部屋を使用しています-見つけました

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

どこで働くか

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

しませんでした

(他の誰かがその問題に直面した場合)

于 2015-06-16T11:17:07.700 に答える