Home Reference Source Test Repository
import ReopeningWebSocket from 'reopening-websocket/src/ReopeningWebSocket.js'
public class | source

ReopeningWebSocket

Extends:

WebSocket → ReopeningWebSocket

Implements:

Automatically reopening WebSocket

Test:

Constructor Summary

Public Constructor
public

constructor(url: String, protocols: String | String[])

Creates a new ReopeningWebSocket

Member Summary

Public Members
public

An event listener to be called on reopen.

public

An event listener to be called on each reopen attempt.

public

How many times a reopen happened without success.

Method Summary

Public Methods
public

addEventListener(type: String, listener: EventListener, options: Boolean | Object)

Register an event handler of a specific type.

public

close(code: Number, reason: String)

Closes the WebSocket connection or connection attempt, if any.

public

Dispatches an Event.

public

Removes an event listener registed with ReopeningWebSocket#addEventListener.

public

reopen()

Reopens the WebSocket connection.

public

Transmits data to the server

Public Constructors

public constructor(url: String, protocols: String | String[]) source

Creates a new ReopeningWebSocket

Params:

NameTypeAttributeDescription
url String

The URL to connect to.

protocols String | String[]
  • optional

The protocol(s) to use.

Public Members

public onreopen: EventListener source

An event listener to be called on reopen.

Test:

public onreopenattempt: EventListener source

An event listener to be called on each reopen attempt.

public reopenAttempt: Number source

How many times a reopen happened without success. Reset to 0 the first time a reopen succeeds.

Public Methods

public addEventListener(type: String, listener: EventListener, options: Boolean | Object) source

Register an event handler of a specific type.

Params:

NameTypeAttributeDescription
type String

The event to listen for.

listener EventListener

The function to call when the event occures.

options Boolean | Object
  • optional

Specifies characteristics of the listener.

options.once Boolean
  • optional

Only call the listener once.

Test:

See:

public close(code: Number, reason: String) source

Closes the WebSocket connection or connection attempt, if any.

Note that after calling this method the connection will not reopen automatically any more. Call ReopeningWebSocket#reopen to reopen it.

Params:

NameTypeAttributeDescription
code Number
  • optional
  • default: 1000

A numeric value indicating why the connection is being closed.

reason String

The human-readable reason why the connection is being closed.

Example:

Disabling and re-enabling automatic reopening
const ws = new ReopeningWebSocket('ws://example.com');
...
ws.close();
// At this point the connection will not no longer automatically reopen
ws.reopen();
// At this point automatic reopening is re-enabled.

Test:

See:

public dispatchEvent(event: Event) source

Dispatches an Event.

Params:

NameTypeAttributeDescription
event Event

The event to dispatch.

Test:

See:

public removeEventListener(type: String, listener: EventListener) source

Removes an event listener registed with ReopeningWebSocket#addEventListener.

Params:

NameTypeAttributeDescription
type String

The event to listen for.

listener EventListener

The function to call when the event occures.

Test:

See:

public reopen() source

Reopens the WebSocket connection.

public send(data: String | ArrayBuffer | Blob) source

Transmits data to the server

Params:

NameTypeAttributeDescription
data String | ArrayBuffer | Blob

The data to transmit.

See: