Websocket框架大小限制 [英] Websocket frame size limitation

查看:963
本文介绍了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.

Side问题:

如何正确地合并破碎的JSON?

How to concatinate a broken JSON properly?

推荐答案

根据 RFC-6455基础框架,单个WebSocket框架具有最大大小限制为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天全站免登陆