node.js 使用 faye 通过 websockets 发送 javascript 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13028604/
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
sending a javascript object through websockets with faye
提问by frx08
Hi all I'm trying to send a javascript object through websockets:
大家好,我正在尝试通过 websockets 发送一个 javascript 对象:
the faye-websockets documentation says:
faye-websockets 文档说:
send(message)accepts either a String or a Buffer and sends a text or binary message over the connection to the other peer.
send(message)接受字符串或缓冲区,并通过连接向其他对等方发送文本或二进制消息。
server side I'm using node and faye.
服务器端我正在使用 node 和 faye。
var WebSocket = require('faye-websocket');
var http = require('http');
var server = http.createServer();
server.addListener('upgrade', function(request, socket, head) {
var ws = new WebSocket(request, socket, head);
ws.send({topic:'handshake', data:'sdf487rgiuh7'});
});
server.listen(8000);
client side:
客户端:
<script>
var ws = new WebSocket('ws://localhost:8000');
ws.onmessage = function(e) {
console.log(e.data); //prints [Object object] string and not the object
};
</script>
what is my error? Thanks
我的错误是什么?谢谢
回答by kanaka
WebSockets support sending and receiving: strings, typed arrays (ArrayBuffer) and Blobs. Javascript objects must be serialized to one of the above types before sending.
WebSockets 支持发送和接收:字符串、类型化数组 (ArrayBuffer) 和 Blob。Javascript 对象在发送之前必须序列化为上述类型之一。
To send an object as a string you can use the builtin JSON support:
要将对象作为字符串发送,您可以使用内置的 JSON 支持:
ws.send(JSON.stringify(object));
To send an object as a typed array you can use a javascript BSON library such as this one:
要发送一个对象作为一个类型数组,你可以使用JavaScript库BSON比如这一个:
ws.send(BSON.serialize(object));
When you receive a WebSocket message you will need to deserialize it.
当您收到 WebSocket 消息时,您需要对其进行反序列化。
To deserialize a JSON string from a WebSocket message:
要从 WebSocket 消息反序列化 JSON 字符串:
ws.onmessage = function (e) {
var object = JSON.parse(e.data);
...
};
If you are using binary messages over WebSocket, then first you should set the binaryType attribute in order to receive all binary messages as typed arrays:
如果您通过 WebSocket 使用二进制消息,那么首先您应该设置 binaryType 属性,以便将所有二进制消息作为类型化数组接收:
ws.binaryType = "arraybuffer";
Then the deserialization will look like this:
然后反序列化将如下所示:
ws.onmessage = function (e) {
var object = BSON.deserialize(e.data);
...
};
Here is a blog post about using BSON in Javascript;
这是一篇关于在 Javascript 中使用 BSON的博客文章;
回答by Alex Ivasyuv
Client:
客户:
const bson = new BSON();
ws.binaryType = 'arraybuffer';
ws.onmessage = function(event) {
console.log(bson.deserialize(Buffer.from(event.data)));
}
Server:
服务器:
const data = bson.serialize({ ... });
ws.send(data);
回答by udidu
I'm basically working with Socket.IO, but it looks like you need to stringify your data in the server and parse it in the client like so:
我基本上使用 Socket.IO,但看起来您需要在服务器中对数据进行字符串化并在客户端中解析它,如下所示:
in the server:
在服务器中:
ws.send(JSON.stringify({topic:'handshake', data:'sdf487rgiuh7'}));
in the client:
在客户端:
console.log(JSON.parse(e.data));

