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

@tulies/websocket-client

v0.0.2

Published

An HTML5 Web Sockets client, to simplify the websocket call on the web side。一个 HTML5 的 Websocket 调用封装库,为了简化 WEB 端 webSocket 调用。

Downloads

16

Readme

介绍

An HTML5 Web Sockets client, to simplify the websocket call on the web side。

一个 HTML5 的 Websocket 调用封装库,为了简化 WEB 端 webSocket 调用。

安装

ES Module Import

yarn add @tulies/websocket-client @tulies/event-emitter
import WebSocketClient from '@tulies/websocket-client'
const wsc = new WebSocketClient('websocket url地址')

引入文件

<script src="/dist/websocket-client.umd.js"></script>
<script>
  var wsc = new WebSocketClient('websocket url地址')
</script>

基础使用

Client 端调用

import WebSocketClient from '@tulies/websocket-client'

const wsc = new WebSocketClient('websocket url地址')

/**  监听websocket的消息回调 **/
// ws-open,ws-close,ws-error,ws-message 为原生的回调封装,请不要自定义占用
// websocket连接后回调
wsc.on('ws-open', (event: Event) => {
  // ...
})
// websocket关闭后回调
wsc.on('ws-close', (event: CloseEvent) => {
  // ...
})
// websocket错误回调
wsc.on('ws-error', (event: Event) => {
  // ...
})
// 最原始的websocket的message
wsc.on('ws-message', (event: MessageEvent) => {
  // ... 建议非特殊需要不用监听这个处理,最合适的是使用双方约定的自定义类型
})


const listener = (data?: any) => {
  // ...
}
// websocket自定义事件注册监听
wsc.on('自定义类型', listener )

/** 移除监听 **/
wsc.off('自定义类型', listener)



/**  websocket消息发送 **/
wsc.emit(type: string, data: any, fn?: (resp?: any) => any): void {
  // ...
}

// 如果在不确定websocket是否连接成功前就想调用emit,请在wsc.ready内调用。
wsc.ready(()=>{
  wsc.emit('自定义类型',{},()=>{...})
})

// 断开连接
wsc.close()
// 重新连接
wsc.reconnect()

Server 端数据规范

json 字符串的数据结构如下:

// 回复前端发送的消息
{
  // type固定为 `reply`
  type:'reply',
  data:{...},
  // 如果是类型是reply,则肯定会有callbackId,否则回调则会无法执行。
  callbackId:'sss'
}

// 全局广播消息
{
  type:'自定义约定的类型',
  data:{...}
}

Dist / Build

Development Build

$ yarn dev

Production Build

$ yarn build