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

huang-can-sheng

v1.0.2

Published

websocket and http server

Readme

概述

该库包提供了两个客户端接口:SocketClient 和 HttpClient,分别用于 WebSocket 通信和 HTTP 请求。以下是对这两个类的详细介绍以及如何使用它们的说明

1、SocketClient

SocketClient 类用于创建一个 WebSocket 客户端,支持重连、心跳机制和消息队列,提供的接口如下:

  • send( data ): 发送消息。如果连接未建立,消息将被加入队列,连接建立后自动发送。
  • close( ): 手动关闭 WebSocket 连接。
  • reconnect( ): 手动触发重连

初始化 SocketClient 实例

初始化 SocketClient 实例时,需要提供一个配置对象。

import { SocketClient } from 'my_package';
const socket = new SocketClient( options = {url……} )

该配置对象包含连接 URL、重连配置、心跳配置等默认参数。

options = {
            url: '', //连接的URL
            messageId: 1, //信息ID
            reconnectCount: 0, //重连次数
            maxReconnect: 5, //最大重连次数
            reconnerInterval: 3000, //重连间隔
            heartbeatInterval: 30000, //心跳间隔
            heartbeatMessage: JSON.stringify({ type: 'ping' }), //心跳信息
            messageQueue: [], //信息队列
            isConnected: false, //是否连接
            heartbeatTimer: null, //心跳计时
            manualClose: false, //手动取消
            onOpen: event => { },  // 连接成功回调函数
            onMessage: event => { }, // 收到消息回调函数
            onClose: event => { }, // 连接关闭回调函数
            onError: event => { }, // 错误处理回调函数
        }

实例化后可以调用暴露的方法,具体方法的介绍上面👆有写,以下是举例

socket.send({ message: 'Hello, WebSocket!' });
socket.close(); // 手动关闭连接 若非手动关闭则会触发内部重连
socket.reconnect(); // 手动触发重连

2、HttpClient

HttpClient 类用于发送 HTTP 请求,支持缓存、拦截器、超时和重试机制,提供的接口如下:

  • config( params = { } ): 配置API接口详情。
  • send( { name, urlParams, body, options = { } } ): 发送 HTTP 请求(支持缓存、拦截器、超时、重试)。
  • setHeader( params = { } ): 设置默认请求头,{ 'Content-Type': 'application/json' } 是默认请求头,默认JSON格式。
  • setTimeout( time ): 设置默认超时时间(毫秒)。
  • setRequestInterceptor( interceptor ): 设置请求拦截器。
  • setResponseInterceptor( interceptor ): 设置响应拦截器。
  • cancel( ): 取消当前请求

初始化 HttpClient 实例

import { HttpClient } from 'my_package';
const http = new HttpClient('https://api.example.com');

实例化后可以调用暴露的方法,具体方法的介绍上面👆有写,以下是举例

http.config({
    getUser: { url: '/users/:id', method: 'GET', responseType: 'json' },
    createUser: { url: '/users', method: 'POST'}
});
http.setHeader({ 'Authorization': 'Bearer your-token' });
http.setTimeout(8000);
http.setRequestInterceptor((config) => {
    // 修改请求配置
    config.headers['X-Custom-Header'] = 'custom-value';
    return config;
});
http.setResponseInterceptor((response) => {
    // 处理响应
    console.log('响应状态码', response.status);
    return response;
});
http.send({
    name: 'getUser',
    urlParams: { id: 1 },
    options: {
        cache: true, // 启用缓存
        retry: 2 // 重试次数
    }
}).then((data) => {
    console.log('响应数据', data);
}).catch((error) => {
    console.log('请求失败', error);
});
http.cancel();

注意事项:

  • 在使用 SocketClient 时,确保 WebSocket 服务器地址正确,并且服务器支持 WebSocket 协议。
  • 在使用 HttpClient 时,注意请求头的设置和响应类型的处理。