在WebSockets中实现permessage-deflate [英] Implementing permessage-deflate in WebSockets

查看:1817
本文介绍了在WebSockets中实现permessage-deflate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WebSockets中理解并实现了一个渗透扩展扩展的问题。

I hava a problem understanding and implementing a permessage-deflate extension in WebSockets.

到目前为止,我添加了'Sec-WebSocket-Extensions:permessage-deflate'内部握手代码。它似乎工作得很好。

So far, I have added 'Sec-WebSocket-Extensions: permessage-deflate' inside handshake code. It seems to work all fine.

然而,当我从服务器(Node.js)向客户端(JS)发送TEST消息时,似乎浏览器(Chrome和Firefox)都没有解压缩数据。

However when I send a "TEST" message from the server (Node.js) to the Client (JS), it seems that the browser (both Chrome and Firefox) is not decompressing the data itself.

如何使用permessage-deflate扩展正确实现数据压缩和解压缩?

How to properly implement data compression and decompression using permessage-deflate extension?

请求标题:

GET ws://localhost/ HTTP/1.1
Host: localhost
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: null
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
Sec-WebSocket-Key: X3RofjiYbzVR8zUPI5ZI6w==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Protocol: Exodus_101

响应标题:

HTTP/1.1 101 Web Socket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
sec-websocket-accept: sFO1Id30BZe63QWcm894hnnb+Pg=
Sec-WebSocket-Protocol: Exodus_101
Sec-WebSocket-Extensions: permessage-deflate


推荐答案

服务器和客户端都使用参数
协商压缩每个消息的WebSocket数据消息的有效载荷数据部分
在打开握手期间

Both, the server and the client compresses the payload data portion of WebSocket data messages on a per-message basis using parameters negotiated during the opening handshake

permessage-deflate 标头在握手中用于指示连接是否应使用压缩。

permessage-deflate header is used in the handshake to indicate whether a connection should use compression.


  1. 当客户端发送websocket请求时,如果客户端浏览器支持,它会在websocket扩展头中发送 permessage-deflate 。服务器知道客户端是否支持基于此标头的压缩。

  2. 如果服务器决定使用压缩,它将使用与ACK消息类似的相同标头进行响应。收到响应后,客户端根据服务器的响应决定是否压缩数据。

  1. When a client sends a websocket request, it send's permessage-deflate in the websocket extensions header IF the client browser supports it. The server knows if a client supports compression based on this header.
  2. If the server decides to use compression, it responds with the same header similar to an ACK message. The client after receiving the response decides whether or not to compress the data based on the server's response.

一旦服务器和客户端同时决定使用压缩,他们个别必须使用deflate压缩技术压缩消息。
即你必须在创建websocket服务器时使用perMessageDeflate选项在服务器上启用压缩。 ws 节点模块默认启用此功能。 ws模块负责标头标志,因此您无需隐式设置它。

Once both the sever and the client decides to use compression, they individually have to compress the message using deflate compression technique. i.e You'll have to enable compression on the server by using the "perMessageDeflate" option while creating the websocket server. the ws node module enables this by default. The ws module takes care of the header flags so that you don't need to implicitly set it.


注意:Deflate使用组合LZ77和Huffman编码压缩
数据。 client_max_window_bits ; server_max_window_bits = 10 标头标志用于设置LZ77算法使用的自定义缓冲区/'滑动窗口',以减少内存开销。

Note: Deflate uses a combination of LZ77 and Huffman coding to compress data.The client_max_window_bits; server_max_window_bits=10 header flags are used to set a custom buffer/'sliding window' used by the LZ77 algorithm to decrease memory overhead.

这篇关于在WebSockets中实现permessage-deflate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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