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

tangyuxian-js-socket

v2.0.0

Published

Encapsulating socket class for TS

Downloads

5

Readme

简介

对websocket进行一次简单的封装,加入心跳和重连机制,开箱即用

使用typescript对原有的1.0版本进行重写,并提供两种安装方式

安装

提供两种安装方式

通过NPM安装

$ npm install/i tangyuxian-js-socket --save

通过CDN安装

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js"></script>

api

URL 对象实例化后的第一个参数是链接通道地址

例如:ws://192.168.1.101:8888/websocket/

option对象中有以下属性

| 参数 | 类型 | 默认值 | 说明 | | ----------------- | -------- | ------ | ------------------------------------------------------------ | | isReconnect | Booble | true | 是否自动重连,启用后自动重连功能生效 | | reconnectTime | Number | 5000 | 在isReconnecttrue时生效,设置每次重连的时间间隔,单位是毫秒 | | reconnectCount | Number | -1 | 重连次数,默认不限制 | | debug | Booble | false | debug模式,开启后会在控制台打印连接情况 | | onOpenAutoSendMsg | Any | | 当连接成功后自动发送的消息内容,一般用于建立连接后发送当前客户端的身份标识用于登录 | | heartTime | Number | 5000 | 心跳时间间隔,单位是毫秒,设置-1时不触发心跳 | | heartMsg | Any | 'ping' | 默认发送的心跳内容 | | openCallback | Function | | 成功连接时的回调函数 | | messageCallback | Function | | 接收到消息的回调函数 | | errorCallback | Function | | 错误的回调函数 | | closeCallback | Function | | 关闭时的回调函数 |

Events

| 事件名 | 参数 | 说明 | | ------------------ | :------------------------------ | ------------------------------------------------------------ | | send | message:Any | 需要发送的数据信息 | | removeSocket | - | 关闭socke连接并标记关闭状态,成功关闭可触发websocket默认关闭事件onclose | | getWebsocket | websocket:Object | 获取实例化之后的websocket对象 | | getActiveLink | type:Booble | 获取当前socket标记状态,当值为false时代表整个socket对象处于不可用状态 | | websocketOnOpen | callback:Function(event:Object) | websocket连接建立成功时的回调函数 | | websocketOnMessage | callback:Function(event:Object) | websocket接收到消息时可触发的回调函数 | | websocketOnError | callback:Function(event:Object) | websocket出现连接错误时触发的回调函数 | | websocketOnClose | callback:Function(event:Object) | websocket关闭时触发的回调函数 |

用法示例

import Socket from 'tangyuxian-js-socket'
let url = "ws://192.168.1.101:8888/websocket"
let option = {
   debug:true,
   onOpenAutoSendMsg:JSON.stringify({id:'123456',type:"login"}),
   openCallback:res=>{
      console.log("建立连接成功",res)
      //...
   },
   messageCallback:res=>{
      console.log("接收到的消息",res) 
      //...
   }
   //...
}
let ws = new Socket(url,option)
//...
ws.removeSocket()
import Socket from 'tangyuxian-js-socket'
let url = "ws://192.168.1.101:8888/websocket"
let ws = new Socket(url)
ws.websocketOnOpen(res=>{
    console.log("建立连接成功",res)
    ws.send(JSON.stringify({id:'123456',type:"login"}))
    //...
})
ws.websocketOnMessage(res=>{
     console.log("接收到的消息",res) 
     //...
})
//...
ws.removeSocket()

许可证

本项目是根据麻省理工学院(MIT)的许可证授权, 详情可点击 LICENSE 文件查看.