簡體   English   中英

websocket可以支持gzip壓縮嗎?

[英]Could websocket support gzip compression?

在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”

默認情況下,在某些瀏覽器中啟用了WebSocket壓縮(例如在Chrome中編寫,但在Firefox中沒有)。 客戶端必須包含'Sec-WebSocket-Extensions:permessage-deflate'標題。 如果服務器以相同的擴展名響應,則WebSocket通信將以幀為基礎進行壓縮。 據我所知,沒有瀏覽器API來啟用/禁用擴展。

關於該主題的一篇好文章是: https//www.igvita.com/2013/11/27/configuring-and-optimizing-websocket-compression/

有一個壓縮擴展正在制作由IETF的WebSocket( HyBi )工作組。 我建議關注他們的郵件列表以獲取最新信息。 我也建議查看這個問題


2017年更新:該擴展程序現已推出一段時間,請訪問: https//tools.ietf.org/html/rfc7692

是的,它可以。 Chrome 19+支持它。

"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()

更多信息: 如何在Autobahn示例中使用PerMessageDeflateOffer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM