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

@xlong/ws

v1.0.2

Published

wangxiaolong utils

Readme

@xlong/ws

简单的 websocket 工具
继承事件 @xlong/events, 实现了心跳检查
自动重连:关闭重连、错误重连、心跳检查失败重连

安装

npm install @xlong/ws 
yarn add @xlong/ws 
pnpm add @xlong/ws 

构造函数

  • new WS(url: string, option?: WsOption)

方法

  • 监听 websocket open 事件:ws.on('open[.openName]',(data)=>{})
  • 监听 websocket message 事件:ws.on('message[.messageName]',(data)=>{})
  • 监听 websocket close 事件:ws.on('close[.closeName]',({code:number})=>{})
  • 监听 websocket error 事件:ws.on('error[.errorName]',({code:number})=>{})
  • ws.close(code:number,reason:string) code:默认1000
  • ws.socket() 重连
  • ws.send(message) 发送消息

属性(继承 websocket 属性)

  • option: WsOption
  • ws: WebSocket
  • queues: []
demo
import WS from "@xlong/ws";

const client = new WS(url);
client.on("message.test", messageFn);
client.on("open.test", openFn);
client.on("close.test", closeFn);
client.on("error.test", errorFn);

client.send();
client.close();
import WS from "@xlong/ws";

const client = new WS(url,{
  // 断开后,或者报错后重连间隔时间
  reConnectTime:3000,
  // 自动发送 ping 到服务器
  autoPing:true,
  // 发送ping 的间隔时间
  pingTime:10000,
  // pong 接受的超出时间,不传则不监控
  timeout:5000,
  // 发送给服务端的 ping 文本,可以为函数
  ping:"ping"
});
client.on("message.test", messageFn);
client.on("open.test", openFn);
client.on("close.test", closeFn);
client.on("error.test", errorFn);

client.send();
client.close();

类型


export interface WsOption {
  protocols?: string | string[];
  reConnectTime?: number;
  /** ping 循环时间 */
  pingTime?: number;
  /** pong 超时时间 */
  timeout?: number;
  autoPing?: boolean;
  /** ping 的数据 */
  ping?: string | Function;
  /** pong 的数据 */
  pong?: string | Function;
  formatData?: (event: MessageEvent) => MessageEvent;
}