> ## Documentation Index
> Fetch the complete documentation index at: https://gowa-docs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# WebSocket 事件

> 将仪表盘连接到 GOWA 的 WebSocket ，处理设备和配对事件，并在重新连接或完成登录后刷新设备状态。

GOWA 通过 `/ws` 推送配对和设备事件。每一帧都包含一个 JSON 对象，其中包含机器可读的 `code`、供人阅读的 `message`，以及结构随事件而异的 `result`。

## 连接

在您的`APP_BASE_PATH`下使用 `/ws`，例如`wss://gowa.example.com/gowa/ws`。选择一个设备与 `?device_id=<id>`。 当基本认证启用时，浏览器客户端传递了 `user:password` 作为`authorization` 的 URL-encoded base64 值。 可以设置 `Authorization` 标题的客户端可以通过该标题使用基本认证。使用 `wss://` 并编辑代理日志中的查询字符串。 如果您分开，将仪表板原始添加到 GOWA 的[受信任的浏览器来源](https://gowa-docs.mintlify.site/zh/authentication.md#cors)。

每个已连接的套接字都会收到每次广播，即使连接时指定了设备也是如此。如果存在 `result.device_id`，可用它识别受影响的槽位。仅向获准访问所有设备的可信调用方授予 WebSocket 访问权限。

## 事件有效负载

| `code` | 到达时 | `result` |
| - | - | - |
| `LIST_DEVICES` | 客户端发送 `FETCH_DEVICES` 。 | 设备列表，包含 `name`、`device` 和 `jid` 条目。 |
| `LOGIN_SUCCESS` | WhatsApp 配对设备。 | `null`。通过REST刷新设备状态。 |
| `PASSKEY_REQUEST` | WhatsApp 请求密钥断言。 | `device_id`, `challenge` (WebAuthn options). |
| `PASSKEY_CONFIRMATION` | WhatsApp 报告密钥关闭结果。 | `device_id`, `code`, `skip_handoff_ux`。当`skip_handoff_ux` 为真时，活动的 QR 流会自动发送确认。 不然的话，将代码与手机进行比较，然后调用 `POST /app/passkey/confirm`。 |
| `PASSKEY_ERROR` | 密码配对失败。 | `device_id`, `continuation`。请从`message`中读取错误文本。 |
| `DEVICE_LOGGED_OUT` | 设备注销并保存其槽位。 | `device_id` 和 `reason` 用于远程注销或`devices` 用于API注销。列表形状取决于注销端点。 |
| `DEVICE_REMOVED` | 设备栏位已删除。 | `device_id`, `devices` (剩余的空档)。 |
| `DEVICE_STREAM_REPLACED` | 另一个客户端连接到相同的 WhatsApp 会话。 | `device_id`。 |
| `DEVICE_TEMPORARY_BAN` | WhatsApp 报告了一个临时封禁。 | 当WhatsApp 提供过期时，`device_id`, `code`, `reason`, `expires_in_seconds`, and `expires_at` 。 |
| `DEVICE_REACHOUT_TIMELOCK` | WhatsApp 报告或取消开始新聊天的限制。 | `device_id`, `is_active`, 和可选的 `enforcement_type` 和 `ends_at`. |
| `DEVICE_WEBHOOK_UPDATED` | 设备的 webhook URL 更改。 | `device_id`, `webhook_url`. |
| `DEVICE_WEBHOOK_CONFIG_UPDATED` | 设备的 webhook 设置更改。 | `device_id`。 |

## 刷新设备状态

若要通过套接字请求设备列表，请以文本帧发送 `{"code":"FETCH_DEVICES"}`。GOWA 会将生成的 `LIST_DEVICES` 事件广播到所有套接字。重新连接或配对后，调用 `GET /devices` 或 `GET /devices/{device_id}/status` 获取当前状态。WebSocket 事件仅即时传递，不会持久保存。

```javascript theme={null}
const url = new URL("wss://gowa.example.com/ws");
url.searchParams.set("device_id", "my-device");
url.searchParams.set("authorization", btoa("admin:changeme"));

const socket = new WebSocket(url);
socket.addEventListener("open", () => {
  socket.send(JSON.stringify({ code: "FETCH_DEVICES" }));
});
socket.addEventListener("message", ({ data }) => {
  const { code, result } = JSON.parse(data);
  if (code === "LIST_DEVICES") {
    console.log("Device list", result);
  }
  if (code === "DEVICE_LOGGED_OUT" && result?.device_id === "my-device") {
    console.log("Device logged out", result.device_id);
  }
});
```

请参阅[密钥配对](https://gowa-docs.mintlify.site/zh/authentication.md#%E5%AF%86%E7%A0%81)的REST 通话，这些通话完成了密钥请求。请使用 [webhooks](https://gowa-docs.mintlify.site/zh/concepts/webhooks.md)处理WhatsApp 消息和收据。


## Related topics

- [认证](https://gowa-docs.mintlify.site/zh/authentication.md)
- [API 认证](https://gowa-docs.mintlify.site/zh/api-reference/authentication.md)
- [多设备与设备选择](https://gowa-docs.mintlify.site/zh/concepts/multidevice.md)
- [Webhooks](https://gowa-docs.mintlify.site/zh/concepts/webhooks.md)
- [API 参考](https://gowa-docs.mintlify.site/zh/api-reference/introduction.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.