> ## Documentation Index
> Fetch the complete documentation index at: https://bun.ll1025.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 为 WebSocket 消息启用压缩

设置 `perMessageDeflate` 参数以使用 [permessage-deflate](https://tools.ietf.org/html/rfc7692) WebSocket 扩展压缩所有消息。

```ts server.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
Bun.serve({
  // ...
  websocket: {
    // 启用压缩
    perMessageDeflate: true,
  },
});
```

***

要为单个消息启用压缩，请向 `ws.send()` 传递 `true` 作为第二个参数。

```ts server.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
Bun.serve({
  // ...
  websocket: {
    async message(ws, message) {
      // 发送压缩消息
      ws.send(message, true);
    },
  },
});
```
