Socket.io 如何发送 JavaScript 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11805130/
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.io how to send JavaScript object
提问by zsd
How to send JavaScript Object with Socket.io
from server to client? I'm using Socket.io as WebSocket(sending with .send()
and listen with message
event).
When I'm trying to do something like on server-side:
如何Socket.io
从服务器向客户端发送 JavaScript 对象?我使用 Socket.io 作为 WebSocket(.send()
使用message
事件发送和监听)。当我尝试在服务器端执行类似操作时:
var myObject = {
message: 'Hello World!'
}
socket.send(myObject);
on client-side I'm getting only this String: [object Object]
在客户端我只得到这个字符串: [object Object]
回答by Brad
You actually need to emit an event instead:
您实际上需要发出一个事件:
socket.emit('yourEvent', myObject);
If you use .send()
, you are simply sending the string representation of your object, which is where the problem is occurring. Note that you can use .send()
, but you would have to JSON-encode the object first, and decode it on reception.
如果您使用.send()
,您只是发送对象的字符串表示,这就是问题发生的地方。请注意,您可以使用.send()
,但您必须先对对象进行 JSON 编码,然后在接收时对其进行解码。
Unless you have a specific reason, it's best to use the standard Socket.IO .emit()
method, as it does all of this for you. That's what it is there for.
除非您有特定原因,否则最好使用标准的 Socket.IO.emit()
方法,因为它会为您完成所有这些工作。这就是它的用途。
回答by Amro
I just ran into this issue using some older example. Here is the answer I found: Migrating 0.6 to 0.7+, which I reproduce below.
我刚刚使用一些较旧的示例遇到了这个问题。这是我找到的答案:Migrating 0.6 to 0.7+,我在下面重现。
In v0.6, socket.send
would automatically convert an object like {a: 'b'}
to JSON. You would send data to a client with:
在 v0.6 中,socket.send
会自动将对象转换{a: 'b'}
为 JSON。您可以通过以下方式向客户端发送数据:
socket.send({a: 'b'});
While this is cool, it poses a fundamental problem. JSON not only encodes objects, but also strings, numbers, etc! So, the API is more clear if you explicitly state you want to pass JSON (since there's a performance penalty associated with encoding/decoding JSON).
虽然这很酷,但它提出了一个根本问题。JSON 不仅可以对对象进行编码,还可以对字符串、数字等进行编码!因此,如果您明确声明要传递 JSON,则 API 会更加清晰(因为编码/解码 JSON 会降低性能)。
In v0.7, use the json
flag:
在 v0.7 中,使用json
标志:
socket.json.send({a: 'b'});
Now you can also emit and receive custom events between the browser and server:
现在您还可以在浏览器和服务器之间发出和接收自定义事件:
socket.emit('my_event', {a: 'b'});
Arguments for events get encoded in JSON automatically for you.
事件参数会自动为您编码为 JSON。
回答by Salman
Try using this on your server-side
尝试在您的服务器端使用它
socket.json.send({ your : 'data' });
socket.json.send({ your : 'data' });
and the JSON.parse()
method on your client-side.
以及JSON.parse()
客户端的方法。
回答by SaliproPham
socket.send()
doesn't understand object but it enjoys with JSON. You can use this way:
socket.send()
不理解对象,但它喜欢 JSON。你可以这样使用:
socket.send(JSON.stringify(myObject));
And use JSON.parse(json)
to parse JSON to Object.
并用于JSON.parse(json)
将 JSON 解析为 Object。