1.0.1 • Published 7 years ago

smart-websocket v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

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}) => {})