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

要为单个消息启用压缩,请将 true 作为第二个参数传递给 ws.send()
https://mintcdn.com/teemo/2s-4Z6VdGqiCeBNX/icons/typescript.svg?fit=max&auto=format&n=2s-4Z6VdGqiCeBNX&q=85&s=087b260066909db1cd3e9c7292bc34b2server.ts
Bun.serve({
  // ...
  websocket: {
    async message(ws, message) {
      // 发送压缩消息
      ws.send(message, true);
    },
  },
});