Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ws-connector.js

Package Overview
Dependencies
Maintainers
2
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ws-connector.js

一个轻量级的websocket连接器插件

  • 1.1.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

ws-connector.js 文档

一个轻量级的 websocket 连接器,简单易用

引入

import Connector from 'ws-connector.js';

使用

//获取实例,传入配置
const connector = Connector.getInstance({
  url: 'wss://examplate.ws.com',
  heartBeat: {
    enable: true,
    sendMessage: 'ping',
    receiveMessage: {
      handle: 'pong',
      receivingStrategy: 'match',
      regExp: /^pong/
    },
    duration: 5000
  }
});

//设置消息
connector.setMessage({
  sub: 'add',
  type: 'quotes'
});

//开始发送
connector.start(receiver /* 接收器 */, () => sendLog('') /* ws中断回调 */);

//接收器
const receiver = ({ data }) => {
  console.log(data);
};

通用配置

配置名称配置说明可选值类型
urlwebsocket url/string
warning是否显示警告/boolean
heartBeat心跳机制配置详见心跳机制配置object

心跳配置

配置名称配置说明可选值类型
enable是否启用心跳/boolean
sendMessage发送的心跳信息/boolean
receiveMessage接收心跳信息的配置/详见接收心跳信息的配置
duration心跳周期,以 ms 为单位,默认为 1000ms/number
breakOffHandler心跳断开的回调/Function

接收心跳信息的配置

配置名称配置说明可选值类型
handle接收句柄/string
receivingStrategy匹配策略match(使用正则匹配句柄),contain(是否包含句柄),absolutely(完全匹配句柄),startsWidth(收到的心跳消息是否以句柄开头),endsWidth(收到的心跳消息是否以句柄结尾)string
regExp匹配正则,只有在匹配策略为"match"时有效,默认为/\*//string

API

Connector.getInstance

创建ws-connector.js实例

connector.setMessage

设置接下来要发送的信息

connector.start

开始接收信息

Keywords

FAQs

Package last updated on 29 Apr 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc