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

usc-ptt-sdk

v1.0.5

Published

usc ptt client

Downloads

4

Readme

usc ptt sdk开发手册

===============

1.安装

//使用yarn安装 
yarn add usc-ptt-sdk
//使用npm安装
npm install usc-ptt-sdk

CDN方式引入

<script type="module">
  import { clientEvent, createPttClient, disableLog, enableLog, pttErrorCode } from 'index.js'
  const client = await createPttClient(ws.value)
</script>

2.简单使用demo

import { clientEvent, createPttClient, pttErrorCode } from 'usc-ptt-sdk'
  const client = await createPttClient(ws.value)
  client.on(clientEvent.ACTIVE_GROUP,(e)=>{
    // e为空字符串时表示当前没有加入任何组,否则e为加入的组的组id
    if(e)
      console.log(`进入组:${e}`)
    else
      console.log('退出组')
  })
  client.on(clientEvent.SPEAKER,(e)=>{
    // e为空字符串时表示当前没有人在说话,否则e为说话人的ptt号
    if(e)
    console.log(`${e}正在说话`)
    else
    console.log(`停止说话`)
  })
  client.on(clientEvent.TALKING,(e)=>{
    // e为true时表示当前自己在说话,否则表示自己停止说话
    if(e)
    console.log(`您正在说话`)
    else
    console.log(`您已停止说话`)
  })
  client.on(clientEvent.JOIN_INFO,(e)=>{
    // 有用户加入组时通知,
    // e的结构如下
      // {
      //   user:8003, 加入的用户号
      //   group:100114, 组id
      //   code:1,  0表示退出组,1表示加入组
      // }
    console.log(`用户加入通知:${JSON.stringify(e)}`)
  })
  // 用户操作
    client.joinGroup('100114','10000008003','10.168.1.17',20023)
    client.quitGroup()
    client.talk()
    client.stop()

3.sdk说明

此项目为javascript项目,以下是一些重要的Api说明:

1.createPttClient

此方法用来初始化一个PttClient客户端,内部已经封装了心跳,消息交互,api描述如下:

import { createPttClient } from "usc-ptt-sdk"
const client = await createPttClient(
  'ws://127.0.0.1:40000', //pttClient连接的地址
  20,                     //心跳间隔(S),非必选,默认20s
  true                    //是否自动重连,非必选,默认true
  )

2.PttClient

PttClient是用来实例化Ptt终端的对象,内部主要方法如下

  • joinGroup(group, user, ptts, port) 加入组
  • quitGroup() 退出组
  • talk() 说话
  • stop() 停止说话

3.ClientEvent

ClientEvent 表示底层封装向上抛出的一些业务与非业务的生命周期事件,具体如下
const clientEvent = {
  ERROR:'error',                  //错误事件
  SPEAKER:'speaker',              //有用户说话或停止说话时触发该事件
  TALKING:'talking',              //自己说话或停止说话时触发该事件
  ACTIVE_GROUP:'activeGroup',     //加入组或退出组时触发该事件
  JOIN_INFO:'joinInfo',           //有用户加入或退出组时触发该事件
}

事例代码

import { clientEvent, createPttClient, pttErrorCode } from 'usc-ptt-sdk'
  const client = await createPttClient('ws://...')
  client.on(clientEvent.ACTIVE_GROUP,(e)=>{
    // e为空字符串时表示当前没有加入任何组,否则e为加入的组的组id
    if(e)
      console.log(`进入组:${e}`)
    else
      console.log('退出组')
  })
  client.on(clientEvent.SPEAKER,(e)=>{
    // e为空字符串时表示当前没有人在说话或当前说话人停止说话,否则e为说话人的ptt号
    if(e)
    console.log(`${e}正在说话`)
    else
    console.log(`停止说话`)
  })
  client.on(clientEvent.TALKING,(e)=>{
    // e为true时表示当前自己在说话,否则表示自己停止说话
    if(e)
    console.log(`您正在说话`)
    else
    console.log(`您已停止说话`)
  })
  client.on(clientEvent.JOIN_INFO,(e)=>{
    // 有用户加入组时通知,
    // e的结构如下
      // {
      //   user:8003, 加入的用户号
      //   group:100114, 组id
      //   code:1,  0表示退出组,1表示加入组
      // }
    console.log(`用户加入通知:${JSON.stringify(e)}`)
  })

请在客户端不用时,调用destory方法销毁客户端(例如路由切换时)