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

ws-heartbeat-ts

v0.6.0

Published

简单封装WebSocket的心跳检测和自动连接

Downloads

14

Readme

ws-heartbeat-ts

According to websocket-heartbeat-js changed TS version.

Why write TS version

  1. Want to try, pack the TS file, and automatically generate .d.ts 😀
  2. Typescript can use singleton mode to avoid the situation that the client creates two identical WS links( version >= 0.5.x)

How to use

  1. Use the getInstance method to get the instance, not the new keyword.
  2. Because this can ensure that for the same URL address, when it is not the initial call, it will get the existing instance, instead of creating a WS connection of the same URL.
/** ts */
import WsHeartBeat, { FWsData } from 'ws-heartbeat-ts';
const wsInstance: WsHeartBeat = WsHeartBeat.getInstance(opt);
wsInstance.onmessage = (data: FWsData) => {
   console.log(data, 'this is ws data');
};

Option

| Attribute | required | type | default | description | | ------ | ------ | ------ | ------ | ------ | | url | true | string | none | websocket service address | | pingTimeout | false | number | 5000 | A heartbeat is sent every 5 seconds. If any backend message is received, the timer will reset | | pongTimeout | false | number | 10000 | After the Ping message is sent, the connection will be disconnected without receiving the backend message within 10 seconds | | reconnectTimeout | false | number | 2000 | The interval of reconnection | | pingMsg | false | string | "{}" | Ping message value | | repeatLimit | false | number | 5 | The trial times of reconnection | | dataType | false | json/byte | 'json' | How to process the returned data | | byteFormat | false| utf-8/iso-8859-2/koi8/cp1261/gbk/etc | 'utf-8'| If datatype is byte, which encoding is used for parsing |

Appointment

  1. If repeatLimit default is null, considering that the new connection will knock down the old connection, if it is not handled, there will be two clients pushing each other and infinitely reconnecting. The back end is required to send the logged in message.
  2. dataType
    interface FWsData {
       code: string; // 识别码
       data: unknown; // 应该用泛型
       type?: string; // 在code码相同的情况下的备用选项
       message?: string; // 需要提示的其他信息
    }

Difference

  1. defaultValue

    1. pingTimeout : 15000 => 5000
    1. pingMsg : heartbeat => {}
  2. params —— Not yet

    1. Add parameters boolean. It may be used in the future
  3. window.setTimeout —— Not yet

    Considering that window.settimeout and window.setinterval will have problems when the browser is minimized, we are ready to switch to worker-timers

  4. interface FWsData,class WsData Front and back end data transfer specification - JSON type

  5. sendData() Add a method to process JSON. sendData can instantiate objects in a fixed format and parse them in the way of WsData. At the same time, the original message event object is reserved as the second return value

  6. Reserved byte transmission mode except JSON format (it may be used if the back end is golong)

  7. others Please see the specific code

Remark

Please refer to websocket-heartbeat-js for other detailed documents

Change declaration file

Just run npm run build in the root of the project.

After packaging, the related declaration file will be generated automatically. .d.ts