Javascript io.emit与socket.emit
我是socket.io的新手,遇到了一些看起来很奇怪的事情。实际上,我不知道Javascript io.emit与socket.emit,javascript,node.js,sockets,socket.io,Javascript,Node.js,Sockets,Socket.io,我是socket.io的新手,遇到了一些看起来很奇怪的事情。实际上,我不知道socket.emit和io.emit之间的区别,但我在任何地方都找不到解释 io.on('connection', function(socket){ io.emit('connected') // <<<< HERE >> socket.emit('connected'); socket.on('disconnect', function(){ io.emit('
socket.emit
和io.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.on('connection',函数(socket){
io.emit('已连接')//变量io
表示套接字组。您的代码从第一行开始,在第二个参数中提供一个函数,该函数在每次建立新连接时为您提供一个socket
变量。socket
变量仅用于与每个单独的连接进行通信。您可能看不到it,但对于每个已建立的连接,将有一个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.
希望这有帮助!这是一个好问题。下面是一个示例代码,可以回答您的问题
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>
发送
var socket=io();
函数发送(msg){
console.log(“发出:+msg”);
emit('send',{“message”:msg});
}
socket.on('new',函数(msg){
console.log(“msg”+msg.message);
$('#messages')。追加($('')。文本(msg.message));
});
$(函数(){
$('form')。提交(函数(e){
e、 预防默认值();
发送($('m').val());
$('m').val('');
返回false;
});
});
在index.htmlsocket.emit('send',{“message”:msg});
这一行代码实际上向服务器发送/发送消息,服务器正在等待侦听socket.on('send',函数(msg){
server.js中的这一行代码
现在,io.sockets.emit('new',msg);
server.js中的这一行将该消息发送到它的所有套接字,并在index.html中使用它的侦听器向用户显示,即,socket.on('new',function(msg){
简单地说,每个套接字将其消息发送到服务器(io是服务器的一个实例),而服务器又将其发送到所有连接的套接字。这就是任何用户发送的消息如何显示给所有用户的方式。我希望这会有所帮助!
socket.emit
将只向发件人发回邮件
io.emit
将
向包括发件人在内的所有客户端发送消息
- 如果你想发送
向所有人发送消息,但不返回发件人,然后
socket.broadcast.emit
当io连接到它时,它会为当前用户的连接创建一个套接字。将其想象为一棵树,io位于树的顶部,并分支成几个套接字。这是一个很好的答案……问题:在我的Node.js服务器端代码中,当我有多个客户端通过socket.io连接时,需要发送相同的数据(JSON)对于所有客户机,如果我使用io.emit vs socket.emit,这是一种更有效、更好的发射方式吗?在第一行,您告诉socket.emit意味着只向发送方客户机发送,但在第九行,您告诉socket.emit();//发送到所有连接的客户端!!!我不明白!实际上这取决于需要。例如,在您的服务器上,您有一行->socket.emit('message','initial message');。这会在加载脚本时触发。因此,最初,所有连接的客户端都会显示“initial message”但是,当您的服务器上有这个代码段->socket.on('message-from-page1',function(message){socket.emit(“message”,“对第1页的响应”);}),并假设客户机发送一个socket.emit('message-from-page1',hi from page1');并且客户机也侦听“message”,则只有该特定的客户机才会收到消息“第1页的响应”行。我为此创建了一个示例(虽然不确定如何在此处共享和模拟),请Ping我以便快速演示。我也很乐意共享代码。当然@prak。让我快速总结并与您共享。请Ping您详细信息:)