通过faye通过websocket发送javascript对象 [英] sending a javascript object through websockets with faye
问题描述
您好,我正在尝试通过websocket发送javascript对象:
faye-websockets文档说:
发送(消息) 接受字符串或缓冲区,并通过连接向另一对等方发送文本或二进制消息.
服务器端,我正在使用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);
客户端:
<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>
我的错误是什么?谢谢
WebSockets支持发送和接收:字符串,类型数组(ArrayBuffer)和Blob.发送之前,必须将Javascript对象序列化为上述类型之一.
要以字符串形式发送对象,可以使用内置的JSON支持:
ws.send(JSON.stringify(object));
要将对象作为类型化数组发送,可以使用javascript BSON库,例如此:
ws.send(BSON.serialize(object));
当您收到WebSocket消息时,需要对其进行反序列化.
要从WebSocket消息反序列化JSON字符串,请执行以下操作:
ws.onmessage = function (e) {
var object = JSON.parse(e.data);
...
};
如果通过WebSocket使用二进制消息,那么首先应该设置binaryType属性,以便将所有二进制消息作为类型化数组接收:
ws.binaryType = "arraybuffer";
然后反序列化将如下所示:
ws.onmessage = function (e) {
var object = BSON.deserialize(e.data);
...
};
这是一篇有关在Javascript中使用BSON的博文; >
Hi all I'm trying to send a javascript object through websockets:
the faye-websockets documentation says:
send(message) accepts either a String or a Buffer and sends a text or binary message over the connection to the other peer.
server side I'm using node and 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
WebSockets support sending and receiving: strings, typed arrays (ArrayBuffer) and Blobs. Javascript objects must be serialized to one of the above types before sending.
To send an object as a string you can use the builtin JSON support:
ws.send(JSON.stringify(object));
To send an object as a typed array you can use a javascript BSON library such as this one:
ws.send(BSON.serialize(object));
When you receive a WebSocket message you will need to deserialize it.
To deserialize a JSON string from a WebSocket message:
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:
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;
这篇关于通过faye通过websocket发送javascript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!