websocket可以支持gzip压缩吗?

时间:2012-07-25 09:28:05

标签: websocket gzip

在WebSocket成功握手之后,我们可以使用gzip压缩吗?

以下是我的测试:

  1. 我使用autobahn lib构建服务器,然后响应客户端:
    HTTP/1.1 101 Switching Protocols content-encoding: gzip Connection: Upgrade Server: AutobahnPython/?.?.? Upgrade: WebSocket Sec-WebSocket-Accept: RIR8KmljoV8Cv9mdiLY7GM2nYMc=
  2. 然后我的服务器使用gzip压缩
  3. 并且chrome浏览器得到了结果,但它告诉我“无法将文本框架解码为UTF-8”

3 个答案:

答案 0 :(得分:9)

默认情况下,某些浏览器启用了WebSocket压缩(例如在Chrome中编写时,但在Firefox中没有)。客户端必须包含'Sec-WebSocket-Extensions:permessage-deflate'标题。如果服务器以相同的扩展名响应,则WebSocket通信将以帧为基础进行压缩。据我所知,没有浏览器API来启用/禁用扩展。

关于该主题的一篇好文章是:https://www.igvita.com/2013/11/27/configuring-and-optimizing-websocket-compression/

答案 1 :(得分:4)

IETF Websocket(compression extension)工作组有worked onHyBi。我建议关注他们的邮件列表以获取最新信息。我还建议您查看this question


2017年更新:该扩展程序现已推出一段时间,请参阅此处:https://tools.ietf.org/html/rfc7692

答案 2 :(得分:0)

是的,它可以。 Chrome 19+ supports it.

"https://github.com/crossbario/autobahn-python/blob/master/examples/twisted/websocket/echo_compressed/server_advanced.py"

from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.static import File

from autobahn.twisted.websocket import WebSocketServerFactory, \
    listenWS

from autobahn.websocket.compress import *

def accept(offers):
    for offer in offers:
        return PerMessageDeflateOfferAccept(offer)

debug = True
factory = WebSocketServerFactory(u"ws://127.0.0.1:9000", debug=debug, debugCodePaths=debug)
factory.setProtocolOptions(perMessageCompressionAccept=accept)

listenWS(factory)

webdir = File(".")
web = Site(webdir)
reactor.listenTCP(8080, web)

reactor.run()

更多信息:how PerMessageDeflateOffer is used in Autobahn examples

相关问题