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

websocket-perfect

v1.1.3

Published

a websocket libary

Downloads

9

Readme

websocket-perfect

npm version

websocket封装,支持业务的重新订阅,断开重连,支持Rxjs6

websocket-perfect 如何使用

npm安装后使用方式如下:

import Ws from 'websocket-perfect'

const ws = new Ws({
  url: 'ws://api.weixiaoyi/ws',
  buffer: true,
  debug: true
})

ws.send({
  subscribe: 'apple',
})
.subscribe(([e, data]) => {
  console.log(data)
})

websocket-perfect API

const ws = new Ws(options)

Options

const ws = new Ws({
  url: 'ws://api.weixiaoyi/ws',
  buffer: true, //所有已订阅的业务是否被缓存起来以当socket断开重连后重新订阅
  bufferSize: 2, // 最大缓存数量
  debug: true, //开启输出log模式
  beforeSend:message=>console.log('开始订阅'),
  afterSend:message=>console.log('订阅完毕')
})

buffer

  • 决定ws.send(message)的参数message是否缓存起来,此配置全局有效,权重最高
  • Accepts true or false
  • Default value: undefiend

bufferWhen

  • 由用户决定ws.send(message)的参数message是否缓存起来,返回true缓存,返回false不缓存,该函数第二个参数,是被拦截器处理过的message,如果没有拦截器,data完全相等于message
  • Accepts (message,data)=>boolean
  • Default value: undefiend

bufferSize

  • 由用户决定ws.send(message)的参数message最大缓存数量
  • Accepts Number
  • Default value: undefiend

beforeSend

  • ws.send(message)之前的钩子函数,该函数第二个参数,是被拦截器处理过的message,如果没有拦截器,data完全相等于message
  • Accepts (message,data)=>void
  • Default value: undefiend

afterSend

  • ws.send(message)之后的钩子函数,该函数第二个参数,是被拦截器处理过的message,如果没有拦截器,data完全相等于message
  • Accepts (message,data)=>void
  • Default value: undefiend

Methods

ws.send(message)

  • 订阅业务,该方法返回结果是一个observable对象,可直接使用Rxjs所有的操作符,调用subscrible方法可立即订阅结果
  • message 订阅参数

ws.close()

  • 客户端主动关闭websoket连接

ws.interceptor(message => message, ([e, data]) => [e,data])

  • websocket配置拦截器,分别处理message发送前的数据和收到响应的数据
  • params[0] 拦截器预处理发送参数
  • params[1] 拦截器预处理响应的数据,参数e是完整的数据,dataedata属性

缓存设置

  1. 开启订阅参数的缓存,必须把buffer设置为true
  2. bufferSize 决定了最大缓存量
  3. bufferWhen 交由用户决定是否缓存该订阅参数
  4. 可以将ws.send(message)方法的参数message添加一个_flush的属性,或者在ws.interceptor拦截器给message添加,带有此属性的message将被自动缓存,该规则权重最低