pop*_*pel 3 javascript json node.js
我在前端有一个对象,我想将它广播到所有连接的客户端.我可以将它作为一个对象发送,就像我定义它一样吗?或者我总是必须在发送之前将它作为JSON对象进行串口化?
我的目标:
var myBox = {
x: 400,
y: 700,
w: 231,
h: 199,
c: "red",
....
}
Run Code Online (Sandbox Code Playgroud)
我需要stringify吗?
var myBox = JSON.stringify({
x: 400,
y: 700,
...
});
Run Code Online (Sandbox Code Playgroud)
目前我发送它像这样,msg是一个JSON:
socket.emit('message', msg);
Run Code Online (Sandbox Code Playgroud)
您可以将对象传递给emit自己而不进行字符串化.它将以纯文本形式发送,但客户端回调将传递一个已解析的对象.
换句话说,这样做很好:
var myBox = {
x: 400,
y: 700,
w: 231,
h: 199,
c: "red"
}
socket.emit('message', myBox);
Run Code Online (Sandbox Code Playgroud)
在客户端收听时,您无需担心JSON.parse:
socket.on('message', function (data) {
alert(data.x);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15807 次 |
| 最近记录: |