com.binance.connector.futures.client.impl.WebsocketClientImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of binance-futures-connector-java Show documentation
Show all versions of binance-futures-connector-java Show documentation
lightweight connector to Futures API
The newest version!
package com.binance.connector.futures.client.impl;
import com.binance.connector.futures.client.WebsocketClient;
import com.binance.connector.futures.client.utils.HttpClientSingleton;
import com.binance.connector.futures.client.utils.RequestBuilder;
import com.binance.connector.futures.client.utils.UrlBuilder;
import com.binance.connector.futures.client.utils.WebSocketCallback;
import com.binance.connector.futures.client.utils.WebSocketConnection;
import com.binance.connector.futures.client.utils.ParameterChecker;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import okhttp3.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Futures Websocket Streams
* All stream endpoints under the
* USDⓈ-M Websocket Market Streams and
* COIN-M Websocket Market Streams and
* USDⓈ-M User Data Streams and
* COIN-M User Data Streams
* section of the API documentation will be implemented in this class.
*
* Response will be returned as callback.
*/
public abstract class WebsocketClientImpl implements WebsocketClient {
private final String baseUrl;
private final Map connections = new HashMap<>();
private final WebSocketCallback noopCallback = msg -> {
};
private static final Logger logger = LoggerFactory.getLogger(WebsocketClientImpl.class);
public WebsocketClientImpl(String baseUrl) {
this.baseUrl = baseUrl;
}
public WebSocketCallback getNoopCallback() {
return this.noopCallback;
}
public String getBaseUrl() {
return this.baseUrl;
}
/**
* The Aggregate Trade Streams push market trade information that is aggregated for fills with same price and taking side every 100 milliseconds.
* Only market trades will be aggregated, which means the insurance fund trades and ADL trades won't be aggregated.
*
* <symbol>@aggTrade
*
* Update Speed: 100ms
*
* @param symbol trading symbol
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Aggregate-Trade-Streams
*/
@Override
public int aggTradeStream(String symbol, WebSocketCallback onMessageCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
return aggTradeStream(symbol, noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #aggTradeStream(String, WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param symbol trading symbol
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int aggTradeStream(String symbol, WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
Request request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/%s@aggTrade", baseUrl, symbol.toLowerCase()));
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* Mark price and funding rate for a single symbol pushed every 3 seconds or every second.
*
* <symbol>@markPrice or <symbol>@markPrice@1s
*
* Update Speed: 3000ms or 1000ms
*
* @param symbol trading symbol
* @param speed speed in seconds, can be 1 or 3
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Mark-Price-Stream
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Mark-Price-Stream
*/
@Override
public int markPriceStream(String symbol, int speed, WebSocketCallback onMessageCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
return markPriceStream(symbol, speed, noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #markPriceStream(String, int, WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param symbol trading symbol
* @param speed speed in seconds, can be 1 or 3
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int markPriceStream(String symbol, int speed, WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
Request request = null;
final int defaultSpeed = 3;
if (speed == defaultSpeed) {
request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/%s@markPrice", baseUrl, symbol.toLowerCase()));
} else {
request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/%s@markPrice@%ss", baseUrl, symbol.toLowerCase(), speed));
}
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* The Kline/Candlestick Stream push updates to the current klines/candlestick every 250 milliseconds (if existing).
*
* <symbol>@kline_<interval>
*
* Update Speed: 250ms
*
* @param symbol trading symbol
* @param interval kline interval - 1m 3m 5m 15m 30m 1h 2h 4h 6h 8h 12h 1d 3d 1w 1M
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Kline-Candlestick-Streams
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Kline-Candlestick-Streams
*/
@Override
public int klineStream(String symbol, String interval, WebSocketCallback onMessageCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
return klineStream(symbol, interval, noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #klineStream(String, String, WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param symbol trading symbol
* @param interval kline interval - 1m 3m 5m 15m 30m 1h 2h 4h 6h 8h 12h 1d 3d 1w 1M
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int klineStream(String symbol, String interval, WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
Request request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/%s@kline_%s", baseUrl, symbol.toLowerCase(), interval));
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* The Kline/Candlestick Stream push updates to the current klines/candlestick every 250 milliseconds (if existing). Contract Types are: perpetual, current_quarter, next_quarter
*
* <pair>_<contractType>@continuousKline_<interval>
*
* Update Speed: 250ms
*
* @param pair trading pair
* @param contractType perpetual, current_quarter, next_quarter
* @param interval kline interval - 1m 3m 5m 15m 30m 1h 2h 4h 6h 8h 12h 1d 3d 1w 1M
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Continuous-Contract-Kline-Candlestick-Streams
*/
@Override
public int continuousKlineStream(String pair, String contractType, String interval, WebSocketCallback onMessageCallback) {
ParameterChecker.checkParameterType(pair, String.class, "pair");
return continuousKlineStream(pair, contractType, interval, noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #continuousKlineStream(String, String, String, WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param pair trading pair
* @param interval kline interval - 1m 3m 5m 15m 30m 1h 2h 4h 6h 8h 12h 1d 3d 1w 1M
* @param contractType perpetual, current_quarter, next_quarter
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int continuousKlineStream(String pair, String contractType, String interval, WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
ParameterChecker.checkParameterType(pair, String.class, "pair");
ParameterChecker.checkParameterType(contractType, String.class, "contractType");
ParameterChecker.checkParameterType(interval, String.class, "interval");
Request request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/%s_%s@continuousKline_%s", baseUrl, pair.toLowerCase(), contractType, interval));
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* 24hr rolling window mini-ticker statistics.
* These are NOT the statistics of the UTC day, but a 24hr rolling window for the previous 24hrs.
*
* <symbol>@miniTicker
*
* Update Speed: 500ms
*
* @param symbol trading symbol
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Individual-Symbol-Mini-Ticker-Stream
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Individual-Symbol-Mini-Ticker-Stream
*/
@Override
public int miniTickerStream(String symbol, WebSocketCallback onMessageCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
return miniTickerStream(symbol, noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #miniTickerStream(String, WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param symbol trading symbol
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int miniTickerStream(String symbol, WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
Request request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/%s@miniTicker", baseUrl, symbol.toLowerCase()));
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* 24hr rolling window mini-ticker statistics for all symbols that changed in an array.
* These are NOT the statistics of the UTC day, but a 24hr rolling window for the previous 24hrs.
* Note that only tickers that have changed will be present in the array.
*
* !miniTicker@arr
*
* Update Speed: 1000ms
*
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Market-Mini-Tickers-Stream
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/All-Market-Mini-Tickers-Stream
*/
@Override
public int allMiniTickerStream(WebSocketCallback onMessageCallback) {
return allMiniTickerStream(noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #allMiniTickerStream(WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int allMiniTickerStream(WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
Request request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/!miniTicker@arr", baseUrl));
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* 24hr rolling window ticker statistics for a single symbol.
* These are NOT the statistics of the UTC day, but a 24hr rolling window for the previous 24hrs.
*
* <symbol>@ticker
*
* Update Speed: 500ms
*
* @param symbol trading symbol
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Individual-Symbol-Ticker-Streams
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Individual-Symbol-Ticker-Streams
*/
@Override
public int symbolTicker(String symbol, WebSocketCallback onMessageCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
return symbolTicker(symbol, noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #symbolTicker(String, WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param symbol trading symbol
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int symbolTicker(String symbol, WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
Request request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/%s@ticker", baseUrl, symbol.toLowerCase()));
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* 24hr rolling window ticker statistics for all symbols.
* These are NOT the statistics of the UTC day, but a 24hr rolling window from requestTime to 24hrs before.
* Note that only tickers that have changed will be present in the array.
*
* !ticker@arr
*
* Update Speed: 1000ms
*
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Market-Tickers-Streams
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/All-Market-Tickers-Streams
*/
@Override
public int allTickerStream(WebSocketCallback onMessageCallback) {
return allTickerStream(noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #allTickerStream(WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int allTickerStream(WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
Request request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/!ticker@arr", baseUrl));
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* Pushes any update to the best bid or ask's price or quantity in real-time for a specified symbol.
*
* <symbol>@bookTicker
*
* Update Speed: Real-time
*
* @param symbol trading symbol
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Individual-Symbol-Book-Ticker-Streams
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Individual-Symbol-Book-Ticker-Streams
*/
@Override
public int bookTicker(String symbol, WebSocketCallback onMessageCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
return bookTicker(symbol, noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #bookTicker(String, WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param symbol trading symbol
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int bookTicker(String symbol, WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
Request request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/%s@bookTicker", baseUrl, symbol.toLowerCase()));
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* Pushes any update to the best bid or ask's price or quantity in real-time for all symbols.
*
* !bookTicker
*
* Update Speed: Real-time
*
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Book-Tickers-Stream
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/All-Book-Tickers-Stream
*/
@Override
public int allBookTickerStream(WebSocketCallback onMessageCallback) {
return allBookTickerStream(noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #allBookTickerStream(WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int allBookTickerStream(WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
Request request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/!bookTicker", baseUrl));
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* The Liquidation Order Snapshot Streams push force liquidation order information for specific symbol.
* For each symbol,only the latest one liquidation order within 1000ms will be pushed as the snapshot.
* If no liquidation happens in the interval of 1000ms, no stream will be pushed.
*
* <symbol>@forceOrder
*
* Update Speed: 1000ms
*
* @param symbol trading symbol
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Liquidation-Order-Streams
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Liquidation-Order-Streams
*/
@Override
public int forceOrderStream(String symbol, WebSocketCallback onMessageCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
return forceOrderStream(symbol, noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #forceOrderStream(String, WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param symbol trading symbol
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int forceOrderStream(String symbol, WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
Request request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/%s@forceOrder", baseUrl, symbol.toLowerCase()));
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* The All Liquidation Order Snapshot Streams push force liquidation order information for all symbols in the market.
* For each symbol,only the latest one liquidation order within 1000ms will be pushed as the snapshot.
* If no liquidation happens in the interval of 1000ms, no stream will be pushed.
*
* !forceOrder@arr
*
* Update Speed: 1000ms
*
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Market-Liquidation-Order-Streams
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/All-Market-Liquidation-Order-Streams
*/
@Override
public int allForceOrderStream(WebSocketCallback onMessageCallback) {
return allForceOrderStream(noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #allForceOrderStream(WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int allForceOrderStream(WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
Request request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/!forceOrder@arr", baseUrl));
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* Top bids and asks, Valid are 5, 10, or 20.
*
* <symbol>@depth<levels>@<speed>ms
*
* Update Speed: 250ms, 500ms or 100ms
*
* @param symbol trading symbol
* @param levels order book depth level, can be 5, 10, or 20
* @param speed update speed in ms, can be 250, 500 or 100
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Partial-Book-Depth-Streams
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Partial-Book-Depth-Streams
*/
@Override
public int partialDepthStream(String symbol, int levels, int speed, WebSocketCallback onMessageCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
return partialDepthStream(symbol, levels, speed, noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #partialDepthStream(String, int, int, WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param symbol trading symbol
* @param levels order book depth level, can be 5, 10, or 20
* @param speed update speed in ms, can be 250, 500 or 100
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int partialDepthStream(String symbol, int levels, int speed, WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
Request request = null;
final int defaultSpeed = 250;
if (speed == defaultSpeed) {
request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/%s@depth%s", baseUrl, symbol.toLowerCase(), levels));
} else {
request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/%s@depth%s@%sms", baseUrl, symbol.toLowerCase(), levels, speed));
}
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* Bids and asks, pushed every 250 milliseconds, 500 milliseconds, 100 milliseconds (if existing)
*
* <symbol>@depth@<speed>ms
*
* Update Speed: 250ms, 500ms, 100ms
*
* @param symbol trading symbol
* @param speed update speed in ms, can be 250, 500 or 100
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Diff-Book-Depth-Streams
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Diff-Book-Depth-Streams
*/
@Override
public int diffDepthStream(String symbol, int speed, WebSocketCallback onMessageCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
return diffDepthStream(symbol, speed, noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #diffDepthStream(String, int, WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param symbol trading symbol
* @param speed update speed in ms, can be 250, 500 or 100
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int diffDepthStream(String symbol, int speed, WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
Request request = null;
final int defaultSpeed = 250;
if (speed == defaultSpeed) {
request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/%s@depth", baseUrl, symbol.toLowerCase(), speed));
} else {
request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/%s@depth@%sms", baseUrl, symbol.toLowerCase(), speed));
}
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* User Data Streams are accessed at /ws/<listenKey>
*
* @param listenKey listen key
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams/Connect
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/user-data-streams/Connect
*/
@Override
public int listenUserStream(String listenKey, WebSocketCallback onMessageCallback) {
return listenUserStream(listenKey, noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #listenUserStream(String, WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param listenKey listen key
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int listenUserStream(String listenKey, WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
Request request = RequestBuilder.buildWebsocketRequest(String.format("%s/ws/%s", baseUrl, listenKey));
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* Combined streams are accessed at /stream?streams=<streamName1>/<streamName2>/<streamName3>
*
* @param streams A list of stream names to be combined
* @param onMessageCallback onMessageCallback
* @return int - Connection ID
* @see
* https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Connect
* @see
* https://developers.binance.com/docs/derivatives/coin-margined-futures/websocket-market-streams/Connect
*/
@Override
public int combineStreams(ArrayList streams, WebSocketCallback onMessageCallback) {
return combineStreams(streams, noopCallback, onMessageCallback, noopCallback, noopCallback);
}
/**
* Same as {@link #combineStreams(ArrayList, WebSocketCallback)} plus accepts callbacks for all major websocket connection events.
*
* @param streams stream name list
* @param onOpenCallback onOpenCallback
* @param onMessageCallback onMessageCallback
* @param onClosingCallback onClosingCallback
* @param onFailureCallback onFailureCallback
* @return int - Connection ID
*/
@Override
public int combineStreams(ArrayList streams, WebSocketCallback onOpenCallback, WebSocketCallback onMessageCallback, WebSocketCallback onClosingCallback, WebSocketCallback onFailureCallback) {
String url = UrlBuilder.buildStreamUrl(String.format("%s/stream", baseUrl), streams);
Request request = RequestBuilder.buildWebsocketRequest(url);
return createConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
}
/**
* Closes a specific stream based on stream Id.
*
* @param connectionId Connection ID
*/
@Override
public void closeConnection(int connectionId) {
if (connections.containsKey(connectionId)) {
connections.get(connectionId).close();
logger.info("Closing Connection ID {}", connectionId);
connections.remove(connectionId);
} else {
logger.info("Connection ID {} does not exist!", connectionId);
}
}
/**
* Closes all streams
*/
@Override
public void closeAllConnections() {
if (!connections.isEmpty()) {
logger.info("Closing {} connections(s)", connections.size());
Iterator> iter = connections.entrySet().iterator();
while (iter.hasNext()) {
WebSocketConnection connection = iter.next().getValue();
connection.close();
iter.remove();
}
}
if (connections.isEmpty()) {
HttpClientSingleton.getHttpClient().dispatcher().executorService().shutdown();
logger.info("All connections are closed!");
}
}
public int createConnection(
WebSocketCallback onOpenCallback,
WebSocketCallback onMessageCallback,
WebSocketCallback onClosingCallback,
WebSocketCallback onFailureCallback,
Request request
) {
WebSocketConnection connection = new WebSocketConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
connection.connect();
int connectionId = connection.getConnectionId();
connections.put(connectionId, connection);
return connectionId;
}
}