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 🙏

© 2025 – Pkg Stats / Ryan Hefner

trap-client-sdk

v1.0.9

Published

JavaScript sdk for trap push server.

Readme

trap-client-sdk

Trap push server JavaScript SDK.

  • 简单易用
  • 提供 React Hook API

📦 安装

npm i trap-client-sdk --save

🔨使用

基础 API

import { TrapClient } from 'trap-client-sdk'
const trapClient = new TrapClient(async () => {
  const { token, endpoint } = await fetch('/getTrapToken').then(resp => resp.json())
  return {
    // appId  必填
    appId: 1,
    // server URL 必填
    serverUrl: endpoint,
    // 连接凭证 必填
    token,
    // 是否显示 debug 信息  默认 false
    debug: false,
    // 自动重连时间 毫秒数 默认 1000
    reconnectInterval: 1000,
    // 重连时间衰减因子 第 n 次重连的时间等于  Math.pow(reconnectDecay, n) * reconnectInterval 默认 1.5
    reconnectDecay: 1.5,
    // 连接超时时间 毫秒数 默认 2000
    timeoutInterval: 2000,
    // 最大重试次数 -1 表示无限重试 默认 -1
    maxReconnectAttempts: -1,
    // 响应超时时间 默认 3000
    responseTimeout: 3000,
  };
})

trapClient.once('ready', () => {
  function room1Listener(msg) =>  {
    console.log('room1', msg)
  }
  
  trapClient.onMessage('room1', room1Listener)

  trapClient.onBroadcast((msg) => {
    console.log('广播消息', msg)
  })
  // 移除事件监听
  // trapClient.removeMessageListener('room1', listener)
})

如果你使用 React Hook

import React, { createContext, Fragment } from 'react'
import { TrapProvider, useTrapReady, useTrapMsgList, useTrapEvent, useTrapEventFn, deserialize } from 'trap-client-sdk'

const TrapContext = createContext()

function App() {
  return (
    // TrapProvider 放到所有使用 trap hook 的组件的根组件上
    <TrapProvider Context={TrapContext} deserializeFn={deserialize.json()} option={async () => {
      const { token, endpoint } = await fetch('/getTrapToken').then(resp => resp.json())
      // 返回 配置对象 配置详情见基础 API
      return { serverUrl: endpoint, appId: 1, debug: true, token }
    }} onAfterDisConnect={(trapClient) => { 
      // 当与服务器断开连接 重新连接
      setTimeout(() => {trapClient.reConnect()}, 3000) 
    }}>
      <Room />
    </TrapProvider>
  )
}


function Room({ key }) {
  // 如果 trap client 已经初始化完成 ready 为 true
  const ready = useTrapReady(TrapContext)
  // 订阅一个 key 返回一个消息数组
  const msgList = useTrapMsgList(TrapContext, key)
  // 订阅一个 key 每次返回最新的消息
  const { count } = useTrapEvent(TrapContext, key + '_count')

  const [count1, setCount1] = useState(0)
  // 订阅一个key 通过回调函数获取最新消息, cancelFn 用于取消订阅
  const cancelFn = useTrapEventFn(TrapContext, key + '_count1', (e) => {
    setCount1(e.body)
  })

  if (!ready) {
    return <div>Loading...</div>
  } else {
    return (
      <Fragment>
        <h1>count: {count}</h1>
        <h1>count1: {count1}</h1>
        <ul>{msgList.map(msg => <li>{msg}</li>)}</ul>
      </Fragment>
    )
  }
}

🤝 参与贡献

$ git clone https://gitlab.weilaijishi.net/trap/sdk.git
$ cd sdk/client-js
$ npm i
$ npm start

// ...Doing some awesome work.
// Send merge request.