smart-websocket

1.0.1 • Public • Published

smart-websocket

npm npm

Features

  • autoReconnect: In non-user disconnected case, the websocket is automatically reconnected after disconnecting
  • smartSend: The message will not be sent until the Websocket is opened

DEMO

DEMO

CODE

Installing

npm i -S smart-websocket

or

yarn add smart-websocket

Usage

import SmartWebsocket from 'smart-websocket'
 
let socket = new SmartWebsocket(URL, OPTIONS)

Options

defaultOptions = {
  autoOpen: true,
  smartSend: true,
  autoReconnect: true,
  reconnectDelay: 500,
  maxReconnectCount: 3
}

Methods

socket.open()
socket.send(data)
socket.close()

Events

socket.addEventListener('connecting', ({reconnectCount}) => {})
socket.addEventListener('open', ({reconnectCount}) => {}) // When the connection is successfully opened, reconnectCount will be set to 0
socket.addEventListener('close', ({reconnectCount}) => {})
socket.addEventListener('error', ({reconnectCount}) => {})
socket.addEventListener('message', ({reconnectCount}) => {})

Readme

Keywords

Package Sidebar

Install

npm i smart-websocket

Weekly Downloads

5

Version

1.0.1

License

MIT

Last publish

Collaborators

  • zack24q