使用 faye 通过 websockets 发送 javascript 对象 [英] sending a javascript object through websockets with faye

查看:25
本文介绍了使用 faye 通过 websockets 发送 javascript 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试通过 websockets 发送一个 javascript 对象:

faye-websockets 文档说:

send(message) 接受字符串或缓冲区,并通过连接向其他对等方发送文本或二进制消息.

服务器端我使用 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'});});服务器.听(8000);

客户端:

我的错误是什么?谢谢

解决方案

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 通过 websockets 发送 javascript 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆