-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.d.ts
79 lines (67 loc) · 2.34 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* An asynchronous WebSocket client.
* @example
* // 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();
*/
export default class WebSocketClient {
_socket: WebSocket;
_closeEvent: CloseEvent | null;
_receiveCallbacksQueue: Array<{ resolve: (data: any) => void, reject: (reason: any) => void }>;
_receiveDataQueue: Array<any>;
/**
* Whether a connection is currently open.
* @returns true if the connection is open.
*/
get connected(): boolean;
/**
* The number of messages available to receive.
* @returns The number of queued messages that can be retrieved with {@link #receive}
*/
get dataAvailable(): number;
/**
* Sets up a WebSocket connection to specified url. Resolves when the
* connection is established. Can be called again to reconnect to any url.
*/
connect(url: string, protocols?: string): Promise<void>;
/**
* Send data through the websocket.
* Must be connected. See {@link #connected}.
*/
send(data: any): void;
/**
* 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 A promise that resolves with the data received.
*/
receive(): Promise<any>;
/**
* 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.
*/
disconnect(code?: number, reason?: string): Promise<CloseEvent | null>;
/**
* Sets up the event listeners, which do the bulk of the work.
*/
private _setupListenersOnConnect(): Promise<void>;
/**
* Resets the receive arrays and close events, called in the constructor
* @private
*/
private _reset(): void;
}