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

ws-connect

v0.0.7

Published

浏览器客户端 WebSocket 连接订阅事件管理库

Downloads

9

Readme

WebSocketConnect

  • 文档还不是很详细,后续有时间再补充。

安装

npm i web-socket-connect -S

创建实例

import WebSocketConnect from 'web-socket-connect';

const wsc = new WebSocketConnect({
    // url: 'wss://www.demo.com/socket?a=1&b=2',

    // url 可以是一个函数,返回 promise
    async url() {
       const res = await fetch('https://www.demo.com/get-socket-url');
       return res.url;
    },
 
    // protocols 同时也可以是一个函数,返回 promise,类似 url
    protocols: ['a1', 'b2'],

    // 返回websocket连接所传输二进制数据的类型 默认 blob 可选 "arraybuffer" | "blob"
    binaryType: 'arraybuffer',
    
    // 处理响应后的数据
    transformResponse(evt: MessageEvent) {
        // 格式化数据后返回
    },

    // 全局事件识别
    recognizer(response) {
        // 识别事件
        if(response.type === 1) {
            return 'eventA'
        }
        return 'eventB'
    }
});

WebSocket 事件监听和移除

wsc.on('open', function (evt) {
  // 
});

wsc.on('message', function (evt) {
  // 
});

wsc.on('error', function (evt) {
  // 
});

wsc.on('close', function (evt) {
  // 
});

// 仅监听一次
wsc.once('open', function () {
 // 
});

// 移除示例
const openListener = (evt) => {
};
wsc.on('open', openListener);
wsc.off('open', openListener); // 移除的函数必须绑定的函数

事件订阅/取消订阅

wsc.subscribe('eventA', function (res) {

});

// 取消订阅示例
function eventAListener(res) {
}
const eventA = wsc.subscribe('eventA', eventAListener);
// 第一种移除方式
eventA.remove();

// 第二种移除方式
wsc.unsubscribe('eventA', eventAListener);


// 更新订阅
eventA.update({
    a: 1,
    b: 2
});

事件定义

wsc.defineEvent('eventA', {
    recognizer(res){
        return res.type === 1; // 事件识别,如果注入了全局的事件识别器,这里的识别器将不会被触发
    },
    subscribe(){}, // 订阅
    unsubscribe(){}, // 取消订阅
    update(){} // 更新
})

发送数据

wsc.send({
    a: 1,
    b: 2
});