# light-websocket-client-ts

> 轻量 Websocket 封装

Latest version **0.0.22** (published 2023-05-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install light-websocket-client-ts
pnpm add light-websocket-client-ts
yarn add light-websocket-client-ts
bun add light-websocket-client-ts
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.22 |
| Published | 2023-05-11 |
| First published | 2023-04-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 47 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | shenweijie@openviewtech.com |
| Maintainers | openviewtech |
| Keywords | websocket, async |

## Links

- npm: https://www.npmjs.com/package/light-websocket-client-ts
- npm.io page: https://npm.io/package/light-websocket-client-ts

## Dependencies (1)

- [buffer](https://npm.io/package/buffer.md) ^6.0.3

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.0.22 (latest) — 2023-05-11
- 0.0.21 — 2023-05-11
- 0.0.20 — 2023-05-11
- 0.0.19 — 2023-05-05
- 0.0.18 — 2023-05-01
- 0.0.17 — 2023-05-01
- 0.0.16 — 2023-05-01
- 0.0.15 — 2023-05-01
- 0.0.14 — 2023-05-01
- 0.0.13 — 2023-05-01
- 0.0.12 — 2023-05-01
- 0.0.11 — 2023-05-01
- 0.0.10 — 2023-05-01
- 0.0.9 — 2023-04-30
- 0.0.8 — 2023-04-30
- … 7 more at https://npm.io/package/light-websocket-client-ts/versions

## README

# light-websocket-client-ts

轻量封装 Websocket 客户端 ts 版

## 简介

一个基于 Websocket 进行简单封装的轻量协议客户端，需配合 `light-websocket-server` 使用

### 1. 连接保活
  
由于 websocket 的 js API 无法发送 websocket 的 ping 帧，导致只能在应用层协议再实现一遍保活机制，所以在 Websocket 数据帧首部分出一个字节作为协议头，实现了相对 websocket 协议更上层，但是相对应用层更下层的保活机制

### 2. 自动重连

- 自动重连默认开启，可以通过 Options 来进一步控制
- 客户端主动调用 disconnect() 关闭连接会禁用自动重连，直到重新调用 connect()
- 自动重连采用退避超时策略，可以通过 Options 来进一步控制
- 收到来自服务端的 `Kickoff` 报文也会禁止重连

## 快速开始

### 浏览器

- 安装依赖

```sh
$ npm install light-websocket-client-ts --saved
```

- 创建连接

```typescript
import {LightWebsocketClientImpl} from 'light-websocket-client-ts';

const client = new LightWebsocketClientImpl('ws://xxx/xxx');
client.onDisconnect(onDisconnect);
client.onConnect(onConnect);
client.onMessage(onMessage);

client.connect();

function onConnect() {
}

function onDisconnect() {
    //每次连接丢失都会回调
}

function onMessage(message: string) {
}
```

### Node.js 端

- 安装依赖

```sh
$ npm install light-websocket-client-ts --saved
$ npm install ws --saved
```

- 创建 Node.js ws 实现

```typescript
import {LightWebsocketClientImpl} from 'light-websocket-client-ts';
import * as Websocket from 'ws';

export class LightWebsocketCLientNodeImpl extends LightWebsocketClientImpl {

    createWebsocket(url: string, protocols?: string[] | undefined): any {
        return new Websocket(url, protocols, {});
    }
}
````

- 使用 Node.js 实现创建连接

```typescript
const client = new LightWebsocketCLientNodeImpl('ws://xxx/xxx');
client.onDisconnect(onDisconnect);
client.onConnect(onConnect);
client.onMessage(onMessage);

client.connect();

function onConnect() {
}

function onDisconnect() {
    //每次连接丢失都会回调
}

function onMessage(message: string) {
}
```

---
_Source: https://npm.io/package/light-websocket-client-ts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
