Coder Social home page Coder Social logo

fitraditya / rewebsocket Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 33 KB

A simple JavaScript WebSocket API wrapper with automatic reconnect function.

Home Page: https://rewebsocket.html5.web.id

License: MIT License

JavaScript 32.62% HTML 67.38%
javascript websocket

rewebsocket's Introduction

ReWebSocket

A simple JavaScript WebSocket API wrapper with automatic reconnect function if the connection was down.

ReWebSocket is fully API compatible. You just need to replace

var ws = new WebSocket(url, protocols);

with

var ws = new ReWebSocket(url, protocols, options);

Installation

Include the javascript, and you're done!

<script src="rewebsocket.min.js"></script>

Initialization

var ws = new ReWebSocket(url, protocols, options);

url

  • The URL to which to connect

protocols (optional)

  • Either a single protocol string or an array of protocol strings. These strings are used to indicate WebSocket sub-protocols.

options (optional)

  • Options (see below)

Options

debug

  • Whether to log debug messages. Debug messages are printed using console.debug().
  • Boolean. Default: false

reconnect

  • Whether to implement automatic reconnect if the connection was down.
  • Boolean. Default: true

interval

  • The number of milliseconds to delay before attempting to reconnect.
  • Integer. Default: 5000

maxAttempts

  • The number of maximum reconnection attempts that will be made before giving up.
  • Integer. Default: 0 (0 = unlimited)

useOfflinejs

Each browsers has its own behaviour to handle WebSocket onclose event. Some of them take a long time (or even never) to trigger onclose event since the connection was down.

  • Whether to use Offline.js to monitor connection status.
  • Boolean. Default: false

Properties

ReWebSocket has similar properties to WebSocket.

  • readyState
  • binaryType

Methods

ReWebSocket has similar methods to WebSocket.

  • send()
  • close()

Additional methods:

  • open(): Manually open WebSocket connection if the connection has fully closed.

Events

ReWebSocket has similar events to WebSocket.

  • onopen
  • onmessage
  • onerror
  • onclose

Additional events:

  • onreopen: Triggered when trying to reopen WebSocket connection if the maximum reconnection attempts has not reached yet.
  • onstop: Triggered when user manually close the WebSocket connection, or maximum reconnection attempts has reached.

Example

<script src="rewebsocket.min.js"></script>
<script>
  var ws = new ReWebSocket('wss://rewebsocket.herokuapp.com');
  ws.onopen = function(event) {
    // do something
  }
  ws.onreopen = function(event) {
    // do something
  }
  ws.onmessage = function(event) {
    // do something
  }
  ws.onerror = function(event) {
    // do something
  }
  ws.onclose = function(event) {
    // do something
  }
  ws.onstop = function(event) {
    // do something
  }
</script>

Documentation

See the documentation here.

rewebsocket's People

Stargazers

Fitra Aditya avatar  avatar  avatar Abdul Jabar Hakim avatar

Watchers

Fitra Aditya avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.