# liwi-resources-websocket-client-node 中文文档

规范地址：https://npmcn.com/package/liwi-resources-websocket-client-node/

版本：15\.2\.0
npm 元数据更新时间：2026-11-03T22:50:32.827Z
中文译文更新时间：2026-11-04T02:24:15.231Z
软件包许可：ISC

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/liwi-resources-websocket-client-node
原文对照：https://npmcn.com/package/liwi-resources-websocket-client-node/?lang=original
项目主页：https://github.com/christophehurpeau/liwi
源码仓库：https://github.com/christophehurpeau/liwi

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：21.3 kB，17 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=22\.18\.0
- 运行依赖：3 个
- 首次发布：2026-09-13T07:09:15.465Z
- 最新版本发布：2026-11-03T22:50:32.547Z
- 已发布版本数：9（近 12 个月 9 个）

## 中文简介

节点websocket客户端实现liwi

## 安装

```sh
npm install liwi-resources-websocket-client-node
```

也可以使用：`pnpm add liwi-resources-websocket-client-node`、`yarn add liwi-resources-websocket-client-node`、`bun add liwi-resources-websocket-client-node`

## README

# liwi-resources-websocket-client-node

节点websocket客户端实现liwi

[![npm version](https://img.shields.io/npm/v/liwi-resources-websocket-client-node.svg?style=flat-square)](https://npmjs.org/package/liwi-resources-websocket-client-node) [![npm downloads](https://img.shields.io/npm/dw/liwi-resources-websocket-client-node.svg?style=flat-square)](https://npmjs.org/package/liwi-resources-websocket-client-node) [![node version](https://img.shields.io/node/v/liwi-resources-websocket-client-node.svg?style=flat-square)](https://npmjs.org/package/liwi-resources-websocket-client-node) [![types](https://img.shields.io/npm/types/liwi-resources-websocket-client-node.svg?style=flat-square)](https://npmjs.org/package/liwi-resources-websocket-client-node)

## 关于

节点 [`TransportClient`](https://npmcn.com/package/liwi-resources-client#transportclient)，用来反对 [`liwi-resources-websocket-server`](https://npmcn.com/package/liwi-resources-websocket-server)。它是 [`liwi-resources-websocket-client`](https://npmcn.com/package/liwi-resources-websocket-client) 为节点主机连接：

-   连接通过 [`ws`](https://github.com/websockets/ws) 而不是node的全局 `WebSocket`，它不能设置握手头，
-   所以 **验证标题** 可以在升级请求上发送，
-   并且禁用了页面可见性感知，因此重新连接永远不会等待 `visibilitychange` 这不会发生。

## 安装

```bash
npm install --save liwi-resources-websocket-client-node
```

## 用法

```ts
import { createNodeWebsocketTransportClient } from "liwi-resources-websocket-client-node";

const transportClient = createNodeWebsocketTransportClient({
  url: "ws://localhost:4005/ws",
  headers: () => ({ authorization: `Bearer ${getToken()}` }),
  onConnectionFailure: (failure) => {
    if (failure.type === "handshake" && failure.status === 401) promptSignIn();
  },
});
transportClient.connect();

const tasksService = createTasksServiceClient(transportClient);
```

## 选项

一切 [`WebsocketTransportClientOptions`](https://npmcn.com/package/liwi-resources-websocket-client#options) 接受,除了 `thirdWebsocketArgument`， `getThirdWebsocketArgument` 和 `visibilitySource`，该包自己设置。加:

| 选项 | 默认的 | 描述 |
| --- | --- | --- |
| `url` | (需要) | Websocket端点。没有 `location` 来推导它 |
| `headers` | \- - - - - - | 握手头，或返回它们的函数 |
| `webSocketOptions` | \- - - - - - | 额外的 [`ws` 客户的选择](https://github.com/websockets/ws/blob/master/doc/ws.md) |
| `webSocketImplementation` | `ws` | `WebSocket` 要使用的构造函数 |

通过 `headers` 作为一个 **函数** 当令牌可以过期时：它在每次连接尝试时进行评估，因此重新连接携带刷新的令牌，而不是在创建时捕获的令牌。

`webSocketOptions.headers` 在下面合并 `headers`,所以 `headers` 在键碰撞中获胜。

## 过期的令牌

被拒绝的升级将通过 `onConnectionFailure` 使用HTTP状态，并且4xx停止重新连接循环，而不是永远重试。看到 [连接失败](https://npmcn.com/package/liwi-resources-websocket-client#connection-failures)。

## 常见问题

### liwi-resources-websocket-client-node 是什么？

节点websocket客户端实现liwi

### 如何安装 liwi-resources-websocket-client-node？

在项目目录运行 npm install liwi-resources-websocket-client-node，或使用 pnpm add / yarn add / bun add。

### liwi-resources-websocket-client-node 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### liwi-resources-websocket-client-node 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### liwi-resources-websocket-client-node 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=22\.18\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### liwi-resources-websocket-client-node 可以商用吗？

liwi-resources-websocket-client-node 使用 ISC 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### liwi-resources-websocket-client-node 还在维护吗？

最新版本 15\.2\.0 发布于 2026/11/04（1 天前）；至今共发布 9 个版本，其中过去 12 个月发布了 9 个；首次发布于 2026/09/13。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

