node.js socket.emit() 与 socket.send()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11498508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
socket.emit() vs. socket.send()
提问by node ninja
What's the difference between these two?
这两者有什么区别?
I noticed that if I changed from socket.emitto socket.sendin a working program, the server failed to receive the message, although I don't understand why.
我注意到,如果我在工作程序中从 更改socket.emit为socket.send,则服务器无法接收消息,尽管我不明白为什么。
I also noticed that in my program if I changed from socket.emitto socket.send, the server receives a message, but it seems to receive it multiple times. When I use console.log()to see what the server received, it shows something different from when I use socket.emit.
我还注意到,在我的程序中,如果我从socket.emit改为socket.send,服务器会收到一条消息,但它似乎收到了多次。当我console.log()用来查看服务器收到的内容时,它显示的内容与我使用socket.emit.
Why this behavior? How do you know when to use socket.emitor socket.send?
为什么会有这种行为?你怎么知道什么时候使用socket.emit或socket.send?
回答by Charles
With socket.emit you can register custom event like that:
使用 socket.emit 您可以注册这样的自定义事件:
server:
服务器:
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
client:
客户:
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
Socket.send does the same, but you don't register to 'news' but to message:
Socket.send 做同样的事情,但你没有注册到“新闻”,而是注册消息:
server:
服务器:
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.send('hi');
});
client:
客户:
var socket = io.connect('http://localhost');
socket.on('message', function (message) {
console.log(message);
});
回答by M.D.
Simple and precise (Source: Socket.IO google group):
简单而精确(来源:Socket.IO google group):
socket.emitallows you to emit custom events on the server and client
socket.emit允许您在服务器和客户端上发出自定义事件
socket.sendsends messages which are received with the 'message'event
socket.send发送随'message'事件接收的消息
回答by Kyle Shay
TL;DR:
特尔;博士:
socket.send(data, callback)is essentially equivalent to calling socket.emit('message', JSON.stringify(data), callback)
socket.send(data, callback)本质上相当于调用 socket.emit('message', JSON.stringify(data), callback)
Without looking at the source code, I would assume that the send function is more efficient edit: for sending string messages, at least?
在不查看源代码的情况下,我会假设 send 函数更有效编辑:对于发送字符串消息,至少?
So yeah basically emit allows you to send objects, which is very handy.
所以是的,emit 基本上允许您发送对象,这非常方便。
Take this example with socket.emit:
以这个例子为例socket.emit:
sendMessage: function(type, message) {
socket.emit('message', {
type: type,
message: message
});
}
and for those keeping score at home, here is what it looks like using socket.send:
对于那些在家里记分的人来说,使用socket.send以下是它的样子:
sendMessage: function(type, message) {
socket.send(JSON.stringify({
type: type,
message: message
}));
}
回答by artch
socket.sendis implemented for compatibility with vanilla WebSocket interface. socket.emitis feature of Socket.IO only. They both do the same, but socket.emitis a bit more convenient in handling messages.
socket.send实现与 vanilla WebSocket 接口兼容。socket.emit仅是 Socket.IO 的功能。它们都做同样的事情,但socket.emit在处理消息时更方便一些。
回答by zhuxy
https://socket.io/docs/client-api/#socket-send-args-ack
https://socket.io/docs/client-api/#socket-send-args-ack
socket.send// Sends a message event
socket.send// 发送消息事件
socket.emit(eventName[, ...args][, ack])// you can custom eventName
socket.emit(eventName[, ...args][, ack])// 您可以自定义事件名称
回答by Krishna Ganeriwal
In basic two way communication systems, socket.emit has proved to be more convincing and easy to use (personal experience) and is a part of Socket.IO which is primarily built for such purposes
在基本的双向通信系统中,socket.emit 已被证明更令人信服且易于使用(个人经验),并且是 Socket.IO 的一部分,主要为此目的而构建

