Skip to main content
可以通过 perMessageDeflate 参数启用逐消息压缩。设置后,所有消息都会使用 permessage-deflate WebSocket 扩展进行压缩。
https://mintcdn.com/ikxin/RzFFGbzo0-4huILA/icons/typescript.svg?fit=max&auto=format&n=RzFFGbzo0-4huILA&q=85&s=a3dffd2241f05776d3bd25171d0c5a79server.ts
Bun.serve({
  // ...
  websocket: {
    // 启用压缩
    perMessageDeflate: true,
  },
});

要为单个消息启用压缩,请在调用 ws.send() 时传入第二个参数 true
https://mintcdn.com/ikxin/RzFFGbzo0-4huILA/icons/typescript.svg?fit=max&auto=format&n=RzFFGbzo0-4huILA&q=85&s=a3dffd2241f05776d3bd25171d0c5a79server.ts
Bun.serve({
  // ...
  websocket: {
    async message(ws, message) {
      // 发送压缩消息
      ws.send(message, true);
    },
  },
});