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-client-socket

v0.4.8

Published

ws客户端

Downloads

3

Readme

ws-client-socket

下载

npm install ws-client-socket -S

测试

本地环境短时间建立连接并发送一条信息(client -> serve -> client):1w连接约3.5s, 1.5w连接约5s

引入

const {ClientSocket} = require('ws-client-socket');
import {ClientSocket} from 'ws-client-socket';

示例

请看example文件夹

实例化化项目

const {ClientSocket} = require('ws-client-socket')
exports.clientSocket = new ClientSocket({
    url: 'ws://127.0.0.1:8989',
    logger: console,
    // socket的相关配置
    socketOptions: {
        wsConfig: {}, // 后端ws库的配置
        wsProtocols: '', // 前端环境 websocket子协议
        reconnectMax: 3, // 默认最多重启次数为3;0:不重启 -1:无限重启
        reconnectTime: 2 * 1000, // 重连时间
        heartCmd: 'heart', // 心跳cmd指令
        heartTime: 5 * 1000, // 心跳时间
    },
    // socket 通信相关配置
    socketSendOptions: {
        // 请求拦截器  把 {cmd: '', uuid:'', content: {}} 转为 其他形式的对象的buffer或string
        reqInterceptors: (req) => {
            const data = {
                uuid: '123456',
                msg_id: req.uuid, //这个自己定义,用于返回信息成功失败信息的时候,来表示是哪一条的结果。
                event_name: req.cmd, //事件名自己定
                content: req.content, //通信内容主体
                app_machine_id: 'appMachineId', //物理工控机的唯一标识
                app_instance_id: 'appInstanceId', //本地软件的实例id,通常可以用软件路径的md5
                app_name: 'parameter_pv', //用于显示的名称,自己app是什么就用什么,中文也可以
                level: 'Info', //info,notice,warnning,error,fatal
                local_time_stamp: Date.now(), //时间精度ms
            };
            // return JSON.stringify(data)
            return Buffer.from(JSON.stringify(data))
        },
        // 响应拦截器 把 其他形式的对象的buffer或string 转为 {cmd: '', uuid:'', content: {}}
        resInterceptors: (data) => {
            // const res = JSON.parse(data.data);

            // const fileReader = new FileReader();
            // //将Blob 对象转换成字符串
            // fileReader.readAsText(res.data, 'utf-8');
            // fileReader.onload = e => {
            //     const res = JSON.parse(fileReader.result.toString());
            // }

            const res = JSON.parse(data.toString())
            return {
                uuid: res.msg_id,
                cmd: res.event_name,
                content: {
                    ...res.content,
                    code: Number(res.content?.code),
                },
            };
        },
    },
});

启动项目

1、直接创建

const init = async () => {
  const wsState = await clientSocket.createSocket();
  console.log(wsState)
}

2、等待创建

let wsState = 0;
clientSocket.createSocket().then(state => {
  wsState = state;
})
const init = async () => {
  // 上面的websocket可能还未连接成功
  // 等待连接三次
  wsState = await clientSocket.waitCreate(3);
  console.log(wsState)
}
init();

通信

1、同步通信

  // 同步 client -> serve -> client
  const {req, res} = await clientSocket.sockSend.send('test', {
    content: {
      name: '张三'
    }
  });

2、回调通信

  // 回调 client -> serve -> client
  clientSocket.sockSend.send('test', {
    content: {
      name: '张三'
    }
  }, ({req, res}) => {
    console.log('我是回调通信')
    console.log(res)
  })

3、订阅通信

   // 订阅 serve -> client
  clientSocket.sockSend.on('test', ({ req, res, socket }) => {
    console.log(res)
  });

  clientSocket.sockSend.on('heart', ({ req, res, socket }) => {
    console.log(res)
  });

  clientSocket.sockSend.send('test', {
    content: {
      name: '张三'
    }
  })
  clientSocket.sockSend.send('test')

监听事件

    clientSocket.on('socket-open', () => {})
    clientSocket.on('socket-error', () => {})
    clientSocket.on('socket-close', () => {})
    clientSocket.on('socket-message', () => {})