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

myws

v2.6.0

Published

A tiny websocket client library.

Downloads

10

Readme

myws

A tiny websocket client library.

Features

  • Automatically reconnect when websocket fails.
  • Support event subscription mode, receive messages by listening to events.
  • Support Typescript.

Installation

# pnpm
$ pnpm add myws

# yarn
$ yarn add myws

# npm
$ npm i myws

Basic Usage

import { initMyws } from 'myws'

let mywsInstance = initMyws({
  open: ture,
  api: 'wss://server/some/path',
  // ...
}, (wsInstance) => {
  // 想要自动重连功能有效的话,你需要使用该回调函数对 ws 示例对象进行赋值更新。
  mywsInstance = wsInstance
})

In Vue2

TIPS: 该方式已不再推荐使用,请使用 initMyws 替代。

import Vue from 'vue'
import { wsInstaller } from 'myws'

Vue.use(wsInstaller, {
  open: true,
  api: 'wss://server/some/path'
  // ...
})

Ws Instance

初始化 WebSocket 后,会返回一个 wsInstance 示例对象。该对象包含以下几个属性。

  1. WS : 即,new WebSocket() 返回的示例对象。
  2. WsBus : 则是一个事件总线,由 mitt 实例化而来,你可以使用它监听 WebSocket 的消息。
  3. connected : (boolean) 表示 WebSocket 是否已完成连接。

Options

| Key | Type | Default Value | Description | | :---: | :---: | :---: | :---: | | open | boolean, string, number | true | 是否开启 ws | | api | string | - | ws 服务地址 | | protocols | string, string[] | undefined | 一个协议字符串或者一个包含协议字符串的数组。 | | heart_interval | number | 50000 | ws 心跳间隔,毫秒数。 | | heart_data | () => any | undefined | ws 心跳时传递的参数。 | | reconnect_interval | number | 3000 | ws 重连间隔,毫秒数。 | | reconnect_limit | number | 30 | 自动重连次数限制。| | reconnect_limit_msg | string | - | 超出重连次数时的提示文本。| | reconnect_msg | string, (count: number) => string | - | 每次尝试重连 ws 时的提示文本, 也可以是一个函数,该函数会被传入当前的重连计数。| | ws_bus_emit_names | Record<string, string> | - | 自定义在响应 ws 消息时,WsBus 的 emit 事件名。| | onmessage | (data: any) => void | - | 接收 message 时回调。| | onopen | (e: Event) => void | - | WebSocket 连接时回调。| | onerror | (e: Event) => void | - | WebSocket 出错时回调。| | onclose | (e: Event) => void | - | WebSocket 关闭时回调。|