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

@ithinkdt/websocket-sdk

v0.1.3

Published

webstart-sdk 的 websocket请求 请求协议的封装

Downloads

16

Readme

@ithinkdt/websocket-sdk

web js插件以及demo, 通过定义一套通信协议及机制,实现web端和本地程序之间的 websocket协议的通信

how to install

For JQuery and original js

Download js lib from npmjs

npm i @ithinkdt/websocket-sdk

add js to html

<script type="text/javascript" src="/webstart-sdk.min.js"></script>

For TypeScript such as Angular Vue React

install

npm i @ithinkdt/websocket-sdk --save-dev

Import

import * as WebSocketSdk from '@ithinkdt/websocket-sdk';

协议规范

  • 通过websocket通信,通信端口:36666,36667,36668。为防止端口冲突,定义三个端口,只要有一个端口正常,即锁定为该端口。

  • 通信发起方与接收方:实时通信,在建立连接之后,双方可以在任意时刻,相互推送信息

SDK

初始化

WebSocketSdk.init(eventHandler, config);
eventHandler(event)

全局的事件处理回调

  • event.type:
    • [ ] log:全局日志
    • [ ] clientStatus:本地程序状态变化
    • [ ] result:执行全局结果
    • [ ] check:检测结果
  • event.message 消息
  • event.data 数据
  • event.status 状态码
function eventHandler(event) {
    if (event.type === 'log') {
      // 日志
    } else if (event.type === 'clientStatus') {
      // 本地程序状态变化
      // event.status:(OFF,ON,INITIAL)
    } else if (event.type === 'result') {
      // 执行全局结果
    } else if (event.type === 'check') {
      // 检测结果执行
    }
}
config 启动配置
{
  // 是否自动启动或下载本地客户端(此项为选择配置项,若不配置或为false,将不会自动下载客户端, 以致agentAdminUrl 配置项失效)
  autoStart: true, // Optional
  // 获取最新客户端版本的地址 (当autoStart为 true时, 此项必须要配置,否则会报错)
  agentAdminUrl: 'pcp://36666/', // Optional
  // websocket 心跳检测频率时间
  heartBeatCheckTimeout: 1000, // Optional 
  // 自定义的websocketurl地址  (此项可选择配置项, 没有配置的情况下会走sdk中的本地地址,【本地地址会在port配置项的端口中轮询,知道成功】, 若配置了此项,则port项就会失效)
  websocketUrl: '',
  port: [36666, 36667, 36668], // Default port Optional 
  domain: '127.0.0.1', // Default domain Optional since V0.1.0
  router: '/ws/', // Default router Optional since V0.1.0
  // 启动后默认调用的一次数据接口(此项为可选择配置项:不配置活配置为空这样初始化之后不会注册控制台 [此项和defaultParam匹配使用])
  defalutCmd: 'statusService.register',
  // 启动后默认调用一次数据的参数(params 此项为可选择配置项)
  defaultParam: {"clientType":"C"},
  // 链接失败后,连续重连最大次数
  reconnectMaxTimes: 100,
  // 是否自动检测客户端版本 (此项为选择配置项; 不配置或为false, 将不会自动检测客户端版本 下面三项配置【appVersion/appVersionUrl/appVersionCmd】将会失效)
  autoCheckAppVersion: true;
  // 客户端版本
  appVersion: '1.2.2',
  // 下载客户端最新版本的地址
  appVersionUrl: '',
  // 检测客户端当前版本的cmd
  appVersionCmd: 'appService.version'
}

发送命令

// 推送的时候,侧传递是哪个值, cmd, param, clientTo (后端定义接口属性: cmd、bizData、clienTo 【属性cmd毕传】)

var cmdId = WebSocketSdk.exec('cmd001', {
  param1: '123',
  param2: '456'
}, {}}, function (res) {
  log('同步返回值' + JSON.stringify(res));
});
if (cmdId !== null) {
  log('执行命令已发送:' + cmdId);
} else {
  log('执行命令发送失败');
}

下载最新客户端版本

/**
 *下载最新版本的客户端
 */
 webSocketSdk.download();

退出程序

/**
 * 退出程序,向后台发送退出命令
 */
WebSocketSdk.exit();

退出通信模式

/**
 * 退出通信,不向后台发送退出命令,仅前端退出
 */
WebSocketSdk.disconnect();

CHANGE LOG

V0.0.7

对心跳检测的调整

V0.0.5

添加 websocketUrl配置项 和 autoCheckAppVersion配置项, 支持一般项目中自定义webscoket的url, 和一般接口的数据处理

v0.0.1

创建@ithinkdt/websocket-sdk