npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

websocket-struct

v1.0.4

Published

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

Downloads

11

Readme

websocket-struct

描述

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

安装

# npm
npm install websocket-struct
# cnpm
cnpm install websocket-struct

示例

直接在你需要定义WS数据结构时引入websocket-struct中的函数即可,然后使用对应的装饰器

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,集中管理