MCPcopy Create free account
hub / github.com/ccxt/node-binance-api / sendWsApiRequest

Method sendWsApiRequest

src/node-binance-api.ts:1637–1676  ·  view source on GitHub ↗

* Send a JSON-RPC request on the WebSocket API connection * @param {string} connectionId - connection identifier * @param {string} method - JSON-RPC method name * @param {object} params - method parameters * @return {Promise} - resolves with the result or rejects with error

(connectionId: string, method: string, params: any = {})

Source from the content-addressed store, hash-verified

1635 * @return {Promise} - resolves with the result or rejects with error
1636 */
1637 sendWsApiRequest(connectionId: string, method: string, params: any = {}): Promise<any> {
1638 return new Promise((resolve, reject) => {
1639 const ws = this.wsApiConnections[connectionId];
1640 if (!ws || ws.readyState !== WebSocket.OPEN) {
1641 reject(new Error('WebSocket API connection not open'));
1642 return;
1643 }
1644
1645 const requestId = this.generateRequestId();
1646 const request = {
1647 id: requestId,
1648 method: method,
1649 params: params
1650 };
1651
1652 const settle = (fn: Function, value: any) => {
1653 const pending = this.wsApiPendingRequests[requestId];
1654 if (!pending) return; // already settled
1655 clearTimeout(pending.timer);
1656 delete this.wsApiPendingRequests[requestId];
1657 fn(value);
1658 };
1659
1660 const timer = setTimeout(() => {
1661 settle(reject, new Error('WebSocket API request timeout'));
1662 }, 30000);
1663
1664 this.wsApiPendingRequests[requestId] = { resolve: (v: any) => settle(resolve, v), reject: (e: any) => settle(reject, e), timer, connectionId };
1665
1666 if (this.Options.verbose) {
1667 this.Options.log('WebSocket API: Sending request:', JSON.stringify(request));
1668 }
1669
1670 ws.send(JSON.stringify(request), (error) => {
1671 if (error) {
1672 settle(reject, error);
1673 }
1674 });
1675 });
1676 }
1677
1678 /**
1679 * Generate a unique request ID for JSON-RPC requests

Callers 4

tickerPriceMethod · 0.95
userDataMethod · 0.95
userMarginDataMethod · 0.95

Calls 2

generateRequestIdMethod · 0.95
logMethod · 0.80

Tested by

no test coverage detected