Websocket 帧大小限制 [英] Websocket frame size limitation

查看:71
本文介绍了Websocket 帧大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 websockets 发送大量 JSON 数据.JSON 可能有超过 1000 个条目.由于帧大小的限制,Websocket 协议会自动将 JSON 拆分为帧,这也无可奈何.因为我们不能改变 websockets 的帧大小.

I'm sending huge chunks of JSON data through websockets. The JSON may have over 1000 entries. Due to the frame size limitation, the Websocket protocol automatically splits the JSON into frames, which cannot be helped. As we cannot change the frame size of websockets.

问题:

当我尝试使用 JSON.parse 评估我的 JSON 时,它给了我一个明显的解析错误,因为帧不是完整的 JSON 对象.所有这些都发生在 Websocket onmessage 事件回调中.如何在不同的帧中接收巨大的 JSON 并仍然能够解析它?我试图在 onmessage 中连接帧,但错误仍然存​​在.

When I try to evaluate my JSON using JSON.parse it gives me a parsing error which is obvious because the frames are not complete JSON objects. All this happens in the Websocket onmessage event callback. How can I recieve the huge JSON in differnt frames and still be able to parse it? I have tried to concat the frames in onmessage still the error persists.

附带问题:

如何正确连接损坏的 JSON?

How to concatinate a broken JSON properly?

推荐答案

单个 WebSocket 框架,根据 RFC-6455 基本框架,最大大小限制为 2^63 字节(9,223,372,036,854,775,807 字节 ~= 9.22 艾字节)(由 @Sebastian)

A single WebSocket frame, per RFC-6455 base framing, has a maximum size limit of 2^63 bytes (9,223,372,036,854,775,807 bytes ~= 9.22 exabytes) (correction by @Sebastian)

但是,由 1 个或多个帧组成的 WebSocket 消息从协议级别对其没有限制.

However, a WebSocket message, made up of 1 or more frames, has no limit imposed on it from the protocol level.

每个 WebSocket 实现都会以不同的方式处理消息和帧限制.例如为整条消息设置最大消息大小(通常是出于内存消耗的原因),或者为大消息提供流选项以更好地利用内存.

Each WebSocket implementation will handle message and frame limits differently. Such as setting maximum messages sizes for whole message (usually for memory consumption reasons), or offering streaming options for large messages to better utilize memory.

但在您的情况下,您选择的 WebSocket 实现可能存在错误,并且错误地将 JSON 消息拆分为多个消息,而不是多个帧.您可以使用 Chrome 中的网络检查工具或 Wireshark 等外部工具来确认此行为.

But in your case, it is likely that your chosen WebSocket implementation has a bug and is improperly splitting up the JSON message into multiple messages, instead of multiple frames. You can use the network inspection tooling in Chrome or an external tool like Wireshark to confirm this behavior.

这篇关于Websocket 帧大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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