[英]io.emit vs socket.emit
我是 socket.io 的新手,遇到了一些看起来很奇怪的事情。 我实际上不知道socket.emit
和io.emit
之间的socket.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,请告诉我。
这里有一份补充文档供参考。
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.
希望这可以帮助!。
io
变量表示套接字组。 您的代码从第一行开始,在第二个参数中提供一个函数,每次建立新连接时都会为您提供一个socket
变量。 socket
变量仅用于与每个单独的连接进行通信。 您可能在代码中看不到它,但是每个建立的连接都会有一个socket
变量
socket.emit
只会向发送者发送回消息,io.emit
将消息发送到所有客户端,包括发送者socket.broadcast.emit
这是个好问题。 这是一个示例代码,可以回答您的问题。
server.js 代码:
// 传入 Socket 连接的监听器
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 });
这行代码实际上向正在等待侦听socket.on('send', function(msg){
server.js 中的这行代码的服务器发送/发出消息。
现在io.sockets.emit('new', msg);
server.js 中的这一行将该消息发送到其所有套接字,并使用其在index.html中的侦听器显示给用户,即socket.on('new', function (msg) {
。
简单地说,每个套接字将其 msg 发送到服务器(io 是服务器的一个实例),而服务器又将其发送到所有连接的套接字。 这就是任何用户发送的 msg 显示给所有用户的方式。 我希望它有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.