-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathwebsocket-streams-connection.ts
More file actions
121 lines (108 loc) · 4.54 KB
/
Copy pathwebsocket-streams-connection.ts
File metadata and controls
121 lines (108 loc) · 4.54 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Portfolio Margin WebSocket Market Streams
*
* Access account information, manage margin positions, and trade with Binance Portfolio Margin.
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
import { WebsocketStreamsBase, WebsocketStream, createStreamHandler } from '@binance/common';
import type { UserDataStreamEventsResponse } from './types';
export class WebsocketStreamsConnection {
private websocketBase: WebsocketStreamsBase;
constructor(websocketBase: WebsocketStreamsBase) {
this.websocketBase = websocketBase;
}
/**
* Adds an event listener for the specified WebSocket event.
* @param event - The WebSocket event to listen for, such as 'open', 'message', 'error', 'close', 'ping', or 'pong'.
* @param listener - The callback function to be executed when the event is triggered. The function can accept any number of arguments.
*/
on(
event: 'open' | 'message' | 'error' | 'close' | 'ping' | 'pong',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
listener: (...args: any[]) => void
) {
this.websocketBase.on(event, listener);
}
/**
* Removes an event listener for the specified WebSocket event.
* @param event - The WebSocket event to stop listening for, such as 'open', 'message', 'error', 'close', 'ping', or 'pong'.
* @param listener - The callback function that was previously added as the event listener.
*/
off(
event: 'open' | 'message' | 'error' | 'close' | 'ping' | 'pong',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
listener: (...args: any[]) => void
) {
this.websocketBase.off(event, listener);
}
/**
* Disconnects from the WebSocket server.
* If there is no active connection, a warning is logged.
* Otherwise, all connections in the connection pool are closed gracefully,
* and a message is logged indicating that the connection has been disconnected.
* @returns A Promise that resolves when all connections have been closed.
* @throws Error if the WebSocket client is not set.
*/
disconnect(): Promise<void> {
return this.websocketBase.disconnect();
}
/**
* Checks if the WebSocket connection is currently open.
* @returns `true` if the connection is open, `false` otherwise.
*/
isConnected(): boolean {
return this.websocketBase.isConnected();
}
/**
* Sends a ping message to all connected Websocket servers in the pool.
* If no connections are ready, a warning is logged.
* For each active connection, the ping message is sent, and debug logs provide details.
* @throws Error if a Websocket client is not set for a connection.
*/
pingServer(): void {
this.websocketBase.pingServer();
}
/**
* Subscribes to one or multiple WebSocket streams
* Handles both single and pool modes
* @param stream Single stream name or array of stream names to subscribe to
* @param id Optional subscription ID
* @returns void
*/
subscribe(stream: string | string[], id?: string): void {
this.websocketBase.subscribe(stream, id);
}
/**
* Unsubscribes from one or multiple WebSocket streams
* Handles both single and pool modes
* @param stream Single stream name or array of stream names to unsubscribe from
* @param id Optional unsubscription ID
* @returns void
*/
unsubscribe(stream: string | string[], id?: string): void {
this.websocketBase.unsubscribe(stream, id);
}
/**
* Checks if the WebSocket connection is subscribed to the specified stream.
* @param stream The name of the WebSocket stream to check.
* @returns `true` if the connection is subscribed to the stream, `false` otherwise.
*/
isSubscribed(stream: string): boolean {
return this.websocketBase.isSubscribed(stream);
}
/**
* Subscribes to the user data WebSocket stream using the provided listen key.
* @param listenKey - The listen key for the user data WebSocket stream.
* @param id - Optional user data stream ID
* @returns A WebSocket stream handler for the user data stream.
*/
userData(listenKey: string, id?: string): WebsocketStream<UserDataStreamEventsResponse> {
return createStreamHandler<UserDataStreamEventsResponse>(this.websocketBase, listenKey, id);
}
}