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

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

问题描述

JSON.stringify显然不是很节省空间。例如,[123456789,123456789]在需要大约5时占用20多个字节。在发送到流之前,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的核心是TEXT或BINARY数据的一组框架。

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

它本身不执行压缩。

然而,WebSocket规范允许Extensions,并且已经有了各种各样的压缩扩展(其中一个的形式化规范已经完成)。

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.

在野外看到的一些扩展:

Some of the extensions seen in the wild:


  • 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(Per-Message Compression Extensions)系列中的第一个,最终将包含其他压缩方案(正在讨论的内容 permessage-bzip2 permessage-lz4 permessage-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天全站免登陆