WebSocket 如何压缩消息? [英] How does WebSocket compress messages?

查看:90
本文介绍了WebSocket 如何压缩消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON.stringify 显然不是很节省空间.例如,[123456789,123456789] 占用 20+ 个字节,而它可能只需要大约 5 个字节.websocket 在发送到流之前是否压缩其 JSON?

JSON.stringify is obviously not very space efficient. For example, [123456789,123456789] occupy 20+ bytes when it could need just around 5. Does websocket compress its JSONs before sending to stream?

推荐答案

WebSocket 本质上只是一组文本或二进制数据的框架.

WebSocket is, at its heart, just a set of framing for TEXT or BINARY data.

它本身不执行压缩.

然而,WebSocket 规范允许扩展,并且已经有多种压缩扩展(其中之一的正式规范已最终确定).

However, the WebSocket spec allows for Extensions, and there have been a variety of compression extensions in the wild (the formalized specs for one of these is finalized).

截至今天(2018 年 8 月),公认的压缩规范为 permessage-deflate.

As of today (August 2018) the accepted compression spec is permessage-deflate.

一些常见的扩展:

  • permessage-deflate - 的名称使用 deflate 压缩整个消息的正式规范,而不管 websocket 帧的数量.
  • x-webkit-deflate-frame - 一种早期提出的压缩方法,用于压缩每个原始 websocket 数据帧.在 Chrome 和 Safari 中使用.(现已在 Chrome 和 Safari 中弃用)
  • perframe-deflate - 上述压缩的重命名版本.在各种 websocket 服务器实现中看到,并且还简要地出现在各种基于 WebKit 的客户端中.(在现代浏览器中完全弃用,但仍会出现在各种 WebSocket 客户端库中)
  • permessage-deflate - the name of the formalized spec for using deflate to compress entire messages, regardless of the number of websocket frames.
  • x-webkit-deflate-frame - an early proposed compression that compresses each raw websocket data frame. Seen in use by Chrome and Safari. (now deprecated in Chrome and Safari)
  • perframe-deflate - a renamed version of the above compression. Seen in use by various websocket server implementations, and also briefly showed up in various WebKit based clients. (Completely deprecated in modern browsers, but does still show up in various WebSocket client libraries)

值得注意的是,permessage-deflate 扩展是 PMCE(每消息压缩扩展)系列中的第一个,最终将包含其他压缩方案(正在讨论的permessage-bzip2permessage-lz4permessage-snappy)

Of note, the permessage-deflate extension is the first in a line of PMCE (Per-Message Compression Extensions) that will eventually include other compression schemes (ones being discussed are permessage-bzip2, permessage-lz4, and permessage-snappy)

这篇关于WebSocket 如何压缩消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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