mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-06-28 14:20:12 +00:00
Added typings for websocket-async (#27293)
* Added typings for websocket-async * Updated in accordance with linter * Update tsconfig.json
This commit is contained in:
committed by
Ryan Cavanaugh
parent
b2bbdd8241
commit
c3293bdfe3
87
types/websocket-async/index.d.ts
vendored
Normal file
87
types/websocket-async/index.d.ts
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
// Type definitions for websocket-async 1.3
|
||||
// Project: https://github.com/jcao219/websocket-async
|
||||
// Definitions by: Ben O'Sullivan <https://github.com/bigbeno37>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// Typescript Version: 2.7
|
||||
|
||||
/**
|
||||
* 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();
|
||||
*/
|
||||
declare class WebSocketClient {
|
||||
_socket: WebSocket;
|
||||
|
||||
_closeEvent: CloseEvent | null;
|
||||
|
||||
_receiveCallbacksQueue: Array<{ resolve: (data: any) => void, reject: (reason: any) => void }>;
|
||||
|
||||
_receiveDataQueue: any[];
|
||||
|
||||
constructor();
|
||||
|
||||
/**
|
||||
* Whether a connection is currently open.
|
||||
* @returns true if the connection is open.
|
||||
*/
|
||||
connected(): boolean;
|
||||
|
||||
/**
|
||||
* The number of messages available to receive.
|
||||
* @returns The number of queued messages that can be retrieved with {@link #receive}
|
||||
*/
|
||||
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 _reset(): void;
|
||||
}
|
||||
|
||||
export default WebSocketClient;
|
||||
26
types/websocket-async/tsconfig.json
Normal file
26
types/websocket-async/tsconfig.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es5",
|
||||
"es2015.promise",
|
||||
"webworker"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"esModuleInterop": true,
|
||||
"strictFunctionTypes": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"websocket-async-tests.ts"
|
||||
]
|
||||
}
|
||||
1
types/websocket-async/tslint.json
Normal file
1
types/websocket-async/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
4
types/websocket-async/websocket-async-tests.ts
Normal file
4
types/websocket-async/websocket-async-tests.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import WebSocketClient from 'websocket-async';
|
||||
|
||||
const connection = new WebSocketClient();
|
||||
connection.connect('');
|
||||
Reference in New Issue
Block a user