# websocket-struct

> **websocket-struct**是一个用于前端和服务器进行`WebScoket`纯数据的传输时，用于字节数据与前端数据结构的互相转换的工具库。

Latest version **1.0.4** (published 2020-11-19) · ISC license · 0 weekly downloads

## Install

```sh
npm install websocket-struct
pnpm add websocket-struct
yarn add websocket-struct
bun add websocket-struct
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2020-11-19 |
| First published | 2020-11-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 37.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | AngelPP |
| Maintainers | angelpp |
| Keywords | ts, websocket, C Struct |

## Links

- npm: https://www.npmjs.com/package/websocket-struct
- npm.io page: https://npm.io/package/websocket-struct

## 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

- 1.0.4 (latest) — 2020-11-19
- 1.0.3 — 2020-11-17
- 1.0.2 — 2020-11-12
- 1.0.1 — 2020-11-09
- 1.0.0 — 2020-11-09

## README

# websocket-struct

## 描述

**websocket-struct**是一个用于前端和服务器进行`WebScoket`纯数据的传输时，用于字节数据与前端数据结构的互相转换的工具库。

## 安装

```bash
# npm
npm install websocket-struct
# cnpm
cnpm install websocket-struct
```

## 示例

直接在你需要定义WS数据结构时引入`websocket-struct`中的函数即可，然后使用对应的`装饰器`：

```typescript
import { WebSocketStruct, tdWebSocketStruct, ReadWrite, tdWebSocketField, FieldType, BitType } from 'websocket-struct';

@tdWebSocketStruct(ReadWrite.All)
export class LED extends WebSocketStruct {
    @tdWebSocketField({ fieldType: FieldType.UInt8 })
    mode = 0;
    @tdWebSocketField({ fieldType: FieldType.Bit, bitType: BitType.Boolean, length: 1, bitDataFromField: 'mode'  })
    supportA = false;
    @tdWebSocketField({ fieldType: FieldType.Bit, bitType: BitType.Boolean, length: 1, bitDataFromField: 'mode'  })
    supportB = false;
    @tdWebSocketField({ fieldType: FieldType.Bit, bitType: BitType.Boolean, length: 1, bitDataFromField: 'mode'  })
    supportC = false;
    @tdWebSocketField({ fieldType: FieldType.Boolean })
    enable = false;
    @tdWebSocketField({ fieldType: FieldType.UInt8 })
    aTime = 0;
    @tdWebSocketField({ fieldType: FieldType.UInt8 })
    bTime = 0;
    @tdWebSocketField({ fieldType: FieldType.UInt8 })
    cTime = 0;
    @tdWebSocketField({ fieldType: FieldType.UInt8_Array, arrLength: 3 })
    reserved = 0; // 3
}

@tdWebSocketStruct(ReadWrite.All)
export class Light extends WebSocketStruct {
    @tdWebSocketField({ fieldType: FieldType.Boolean })
    enable = false;
    @tdWebSocketField({ fieldType: FieldType.Struct, construct: LED })
    micOn = new LED();
    @tdWebSocketField({ fieldType: FieldType.UInt8_Array, arrLength: 32 })
    reserved = 0;
}

...

// 读数据
const buffer = [...];
const light = new Light();
light.fromBuffer(buffer);
// 写数据
const buffer2 = light.toBuffer();
```

## 主要API

装饰器、类：

| 名字              | 类型     | 描述                                             |
| ----------------- | -------- | ------------------------------------------------ |
| tdWebSocketStruct | Function | 装饰器，装饰WS类                                 |
| tdWebSocketField  | Function | 装饰器，装饰WS类的字段                           |
| WebSocketStruct   | Class    | 所有WS类的父类，所有继承了这个类的子类称为`WS类` |



可读写属性：

| 名字               | 描述   |
| ------------------ | ------ |
| ReadWrite.Readable | 只读   |
| ReadWrite.Writable | 只写   |
| ReadWrite.All      | 可读写 |



WS类字段的装饰器函数参数的类型`TdWebSocketFieldParameter`：

| 名字               | 类型      | 描述                                                         |
| ------------------ | --------- | ------------------------------------------------------------ |
| fieldType          | FieldType | 详细可看支持性                                               |
| length             | number    | 字节长度，number表示指定长度                                 |
| arrLength          | number    | 数组长度，number表示指定长度，对于`*_Array`类型的数据，必需传入此参数 |
| arrLengthFromField | string    | 指定你要从哪个字段中读取arrLength的值                        |
| bitDataFromField   | string    | 指定你要从哪个字段中读取bit数据。当未指定时，将分析字节数据对应的二进制形式。读取多长的字由length决定 |
| bitType            | BitType   | 详细可看支持性                                               |
| reverse            | boolean   | 对于`Boolean`类型的数据，结果是否取反，true-是，false-否     |
| construct          | Class     | 对于`Struct`类型的数据，必需传入此参数                       |



## 支持性

`FieldType`类型：

| 名字           | 描述                      |
| -------------- | ------------------------- |
| UInt8          | 1 byte，无符号            |
| UInt16         | 2 bytes，无符号           |
| UInt32         | 4 bytes，无符号           |
| UInt64         | 8 bytes，无符号           |
| UInt8_Array    | UInt8数组                 |
| UInt16_Array   | UInt16数组                |
| UInt32_Array   | UInt32数组                |
| UInt64_Array   | UInt64数组                |
| Int8           | 1 byte，有符号            |
| Int16          | 2 bytes，有符号           |
| Int32          | 4 bytes，有符号           |
| Int64          | 8 bytes，有符号           |
| Int8_Array     | Int8数组                  |
| Int16_Array    | Int16数组                 |
| Int32_Array    | Int32数组                 |
| Int64_Array    | Int64数组                 |
| Bit            | 1 bit                     |
| Boolean        | 布尔值，可以指定`reverse` |
| Boolean_Array  | Boolean数组               |
| Float32        | 4 bytes，浮点数           |
| Float32_Array  | Float32数组               |
| Float64        | 8 bytes，浮点数           |
| Float64_Array  | Float64数组               |
| Char           | ASCII字符串               |
| Char_Array     | Char数组                  |
| Utf8Char       | UTF-8字符串               |
| Utf8Char_Array | Utf8Char数组              |
| Struct         | 自定义数据结构（WS类）    |
| Struct_Array   | Struct数组                |



`BitType`类型：

| 名字    | 描述        |
| ------- | ----------- |
| Number  | number类型  |
| Boolean | boolean类型 |



## 特点

- 良好的TS类型提示
- 使用简单，而且支持的数据类型很多
- 减少大量的冗余代码，开发者只需要关心业务
- 减少服务器修改属性后带来的维护成本，前端只需要维护一份model，集中管理

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