websocket-async
Version:
An async/await WebSocket client for browsers
90 lines (60 loc) • 3.01 kB
Markdown
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
### Table of Contents
- [WebSocketClient](#websocketclient)
- [connected](#connected)
- [dataAvailable](#dataavailable)
- [connect](#connect)
- [send](#send)
- [receive](#receive)
- [disconnect](#disconnect)
## WebSocketClient
An asynchronous WebSocket client.
**Examples**
```javascript
// Set up connection.
const webSocketClient = new WebSocketClient;
// Connect.
await webSocketClient.connect('ws://www.example.com/');
// Send is synchronous.
webSocketClient.send('Hello!');
// Receive is asynchronous.
console.log(await webSocketClient.receive());
// See if there are any more messages received.
if (webSocketClient.dataAvailable !== 0) {
console.log(await webSocketClient.receive());
}
// Close the connection.
await webSocketClient.disconnect();
```
### connected
Whether a connection is currently open.
Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true if the connection is open.
### dataAvailable
The number of messages available to receive.
Returns **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** The number of queued messages that can be retrieved with [#receive](#receive)
### connect
Sets up a WebSocket connection to specified url. Resolves when the
connection is established. Can be called again to reconnect to any url.
**Parameters**
- `url` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
- `protocols` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<void>**
### send
Send data through the websocket.
Must be connected. See [#connected](#connected).
**Parameters**
- `data` **any**
### receive
Asynchronously receive data from the websocket.
Resolves immediately if there is buffered, unreceived data.
Otherwise, resolves with the next rececived message,
or rejects if disconnected.
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<any>** A promise that resolves with the data received.
### disconnect
Initiates the close handshake if there is an active connection.
Returns a promise that will never reject.
The promise resolves once the WebSocket connection is closed.
**Parameters**
- `code` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `reason` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[CloseEvent](https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent)?>**