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 🙏

© 2026 – Pkg Stats / Ryan Hefner

socket-web

v0.1.1

Published

webSocket client

Readme

socket-web

浏览器端使用的 webSocket 客户端

安装

npm i socket-web -S

// 或

yarn add socket-web

使用

import SocketWeb from 'socket-web'

const ws = new SocketWeb('wss://example.com')

ws.on('message', ({ data }: MessageEvent) => {
  console.log('new message:', data)
})

配置

语法

const ws = new SocketWeb(url [, protocols, options])

url

Websocket 参数一致

要连接的URL;这应该是WebSocket服务器将响应的URL。

protocols

Websocket 参数一致

一个协议字符串或者一个包含协议字符串的数组。这些字符串用于指定子协议,这样单个服务器可以实现多个WebSocket子协议(例如,您可能希望一台服务器能够根据指定的协议(protocol)处理不同类型的交互)。如果不指定协议字符串,则假定为空字符串。

options

interface Options {
  debug?: boolean, // 是否输出调试信息,默认 false
  autoConnect?: boolean, // 是否在实例化时立即连接,默认 true

  // 断开重新连接配置
  reconnect?: {
    enable?: boolean, // 是否启用,默认 false
    interval?: number, // 重连之前 延迟时间间隔,单位 ms,默认 1000
    maxInterval?: number, // 重连之前 最大 延迟时间间隔,单位 ms,默认 30000
    decay?: number, // 重新连接延迟的增加速率 - 每次重连的延迟时间是上次的 decay 倍,默认 2
    maxNumber?: number // 最大重连次数 - 默认 Infinity(无穷大,不限制)
  },

  // 心跳配置
  heartbeat?: {
    enable?: boolean, // 是否启用,默认 false
    interval?: number, // 心跳间隔,单位 ms,默认 30000
    maxRetryNumber?: number, // 最大重试次数 - 超过后重连(需启用断开重连),默认 0(下一次发送心跳时若上一次心跳无响应则关闭连接,若开启重连,则会在断开后重新连接)
    sendContent?: string, // 自定义心跳发送内容,默认 ping
    receiveContent?: string // 自定义心跳接收内容, 默认 pong
  }
}

API

方法

ws.connect

连接 WebSocket,默认在实例化时立即连接,当autoConnect=false时可以使用

ws.reconnectStart

开始重新连接 WebSocket(需启用断开重连),若当前连接未断开则会先断开当前连接

ws.send

发送消息,在 WebSocket.send() 基础上增加支持传入JavaScript 对象,在实际发送前先会使用 JSON.stringify 转换为 JSON 字符串

ws.send('hello')

type Send = (data: string | ArrayBufferLike | Blob | ArrayBufferView | Record<any, any>) => void

ws.close

关闭 WebSocket 连接,与 WebSocket.close() 一致

ws.close()

type Close = (code?: number, reason?: string) => void

事件

ws.on('open', function(Event))

ws.on('open', (event: Event) => {
  console.log('open:', event)
})

ws.on('message', function(MessageEvent))

ws.on('message', (event: MessageEvent) => {
  console.log('message:', event)
})

ws.on('close', function(CloseEvent))

ws.on('close', (event: CloseEvent) => {
  console.log('close:', event)
})

ws.on('error', function(Event))

ws.on('error', (event: Event) => {
  console.log('error:', event)
})