io.emit 对比 socket.emit

io.emit vs socket.emit

我是 socket.io 的新手并且 运行 了解一些看起来很奇怪的东西。我实际上不知道 socket.emitio.emit 之间的区别,但我在任何地方都找不到解释。

io.on('connection', function(socket){
  io.emit('connected')  // <<<< HERE >> socket.emit('connected');
  socket.on('disconnect', function(){
    io.emit('disconnect')
  });
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});

server.listen(3000);

那是我的服务器内容,但是当我将 io 更改为 socket 时,该消息仅在正在连接的用户连接时显示。 io.emit 将消息发送给所有用户。

也许它应该是那样的,或者它只是一些可怕的黑客攻击?如果您需要客户端,请告诉我 HTML.

io 变量表示套接字组。您拥有的代码从第一行开始,在第二个参数中提供一个函数,每次建立新连接时都会为您提供一个 socket 变量。 socket 变量仅用于与每个单独的连接进行通信。您可能在代码中看不到它,但每个建立的连接都会有一个 socket 变量

补充文档供参考:

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 文档也包含类似的 cheatsheet

这是个好问题。这是一个示例代码,可能会回答您的问题。

server.js代码:

// 传入套接字连接的侦听器

io.on('connection', function(socket){
  socket.on('send', function(msg){
    console.log('message received/sending: ' + msg);
    io.sockets.emit('new', msg);
  });
});

index.html代码

<body>
    <ul id="messages"></ul>
    <form action="">
        <input id="m" autocomplete="off" />
        <button type="submit">Send</button>
    </form>
    <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
    <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
    <script>
        var socket = io();
        function send(msg) {
            console.log("emitting: " + msg);
            socket.emit('send', { "message": msg });
        }

        socket.on('new', function (msg) {
            console.log("msg " + msg.message);
            $('#messages').append($('<li>').text(msg.message));
        });

        $(function () {
            $('form').submit(function (e) {
                e.preventDefault();
                send($('#m').val());
                $('#m').val('');
                return false;
            });
        });
    </script>
</body>

index.html socket.emit('send', { "message": msg }); 这行代码实际上sends/emits 向正在等待收听socket.on('send', function(msg){ 的服务器发送消息server.js中的这行代码。

现在 io.sockets.emit('new', msg); 来自 server.js 的这一行向其所有套接字发出该消息,并使用其在 中的侦听器显示给用户index.htmlsocket.on('new', function (msg) {.

简单地说,每个套接字将其消息发送到服务器(io 是服务器的一个实例),然后服务器将其发送到所有连接的套接字。这就是任何用户发送的消息显示给所有用户的方式。希望对您有所帮助!

  • socket.emit 只会将消息发回给发件人,
  • io.emit 会 向包括发件人在内的所有客户端发送消息
  • 如果你想发送 然后向所有人发送消息但不返回给发件人 socket.broadcast.emit