Skip to content

Commit 8d38e8e

Browse files
authored
Merge pull request binance-exchange#264 from jmresendiz/feat/onBookTickerEvent
Add WebSocket BookTickerEvent
2 parents c916c2c + 2b6eaf1 commit 8d38e8e

File tree

3 files changed

+133
-10
lines changed

3 files changed

+133
-10
lines changed

src/main/java/com/binance/api/client/BinanceApiWebSocketClient.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package com.binance.api.client;
22

3-
import com.binance.api.client.domain.event.AggTradeEvent;
4-
import com.binance.api.client.domain.event.AllMarketTickersEvent;
5-
import com.binance.api.client.domain.event.CandlestickEvent;
6-
import com.binance.api.client.domain.event.DepthEvent;
7-
import com.binance.api.client.domain.event.UserDataUpdateEvent;
3+
import com.binance.api.client.domain.event.*;
84
import com.binance.api.client.domain.market.CandlestickInterval;
95

106
import java.io.Closeable;
@@ -60,6 +56,15 @@ public interface BinanceApiWebSocketClient extends Closeable {
6056
*/
6157
Closeable onAllMarketTickersEvent(BinanceApiCallback<List<AllMarketTickersEvent>> callback);
6258

59+
/**
60+
* Open a new web socket to receive {@link BookTickerEvent bookTickerEvents} on a callback.
61+
*
62+
* @param symbols market (one or coma-separated) symbol(s) to subscribe to
63+
* @param callback the callback to call on new events
64+
* @return a {@link Closeable} that allows the underlying web socket to be closed.
65+
*/
66+
Closeable onBookTickerEvent(String symbols, BinanceApiCallback<BookTickerEvent> callback);
67+
6368
/**
6469
* @deprecated This method is no longer functional. Please use the returned {@link Closeable} from any of the other methods to close the web socket.
6570
*/
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.binance.api.client.domain.event;
2+
3+
import org.apache.commons.lang3.builder.ToStringBuilder;
4+
5+
import com.binance.api.client.constant.BinanceApiConstants;
6+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
9+
/**
10+
* BookTickerEvent event for a symbol. Pushes any update to the best bid or
11+
* ask's price or quantity in real-time for a specified symbol.
12+
*/
13+
@JsonIgnoreProperties(ignoreUnknown = true)
14+
public class BookTickerEvent {
15+
16+
@JsonProperty("u")
17+
private long updateId;
18+
19+
@JsonProperty("s")
20+
private String symbol;
21+
22+
@JsonProperty("b")
23+
private String bidPrice;
24+
25+
@JsonProperty("B")
26+
private String bidQuantity;
27+
28+
@JsonProperty("a")
29+
private String askPrice;
30+
31+
@JsonProperty("A")
32+
private String askQuantity;
33+
34+
public BookTickerEvent() {
35+
super();
36+
}
37+
38+
public BookTickerEvent(long updateId, String symbol, String bidPrice, String bidQuantity, String askPrice,
39+
String askQuantity) {
40+
super();
41+
this.updateId = updateId;
42+
this.symbol = symbol;
43+
this.bidPrice = bidPrice;
44+
this.bidQuantity = bidQuantity;
45+
this.askPrice = askPrice;
46+
this.askQuantity = askQuantity;
47+
}
48+
49+
public BookTickerEvent(String symbol, String bidPrice, String bidQuantity, String askPrice, String askQuantity) {
50+
super();
51+
this.symbol = symbol;
52+
this.bidPrice = bidPrice;
53+
this.bidQuantity = bidQuantity;
54+
this.askPrice = askPrice;
55+
this.askQuantity = askQuantity;
56+
}
57+
58+
public long getUpdateId() {
59+
return updateId;
60+
}
61+
62+
public void setUpdateId(long updateId) {
63+
this.updateId = updateId;
64+
}
65+
66+
public String getSymbol() {
67+
return symbol;
68+
}
69+
70+
public void setSymbol(String symbol) {
71+
this.symbol = symbol;
72+
}
73+
74+
public String getBidPrice() {
75+
return bidPrice;
76+
}
77+
78+
public void setBidPrice(String bidPrice) {
79+
this.bidPrice = bidPrice;
80+
}
81+
82+
public String getBidQuantity() {
83+
return bidQuantity;
84+
}
85+
86+
public void setBidQuantity(String bidQuantity) {
87+
this.bidQuantity = bidQuantity;
88+
}
89+
90+
public String getAskPrice() {
91+
return askPrice;
92+
}
93+
94+
public void setAskPrice(String askPrice) {
95+
this.askPrice = askPrice;
96+
}
97+
98+
public String getAskQuantity() {
99+
return askQuantity;
100+
}
101+
102+
public void setAskQuantity(String askQuantity) {
103+
this.askQuantity = askQuantity;
104+
}
105+
106+
@Override
107+
public String toString() {
108+
return new ToStringBuilder(this, BinanceApiConstants.TO_STRING_BUILDER_STYLE).append("eventType", "BookTicker")
109+
.append("updateId", updateId).append("symbol", symbol).append("bidPrice", bidPrice)
110+
.append("bidQuantity", bidQuantity).append("askPrice", askPrice).append("askQuantity", askQuantity)
111+
.toString();
112+
}
113+
}

src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
import com.binance.api.client.BinanceApiWebSocketClient;
55
import com.binance.api.client.config.BinanceApiConfig;
66
import com.binance.api.client.constant.BinanceApiConstants;
7-
import com.binance.api.client.domain.event.AggTradeEvent;
8-
import com.binance.api.client.domain.event.AllMarketTickersEvent;
9-
import com.binance.api.client.domain.event.CandlestickEvent;
10-
import com.binance.api.client.domain.event.DepthEvent;
11-
import com.binance.api.client.domain.event.UserDataUpdateEvent;
7+
import com.binance.api.client.domain.event.*;
128
import com.binance.api.client.domain.market.CandlestickInterval;
139
import com.fasterxml.jackson.core.type.TypeReference;
1410

@@ -67,6 +63,15 @@ public Closeable onAllMarketTickersEvent(BinanceApiCallback<List<AllMarketTicker
6763
return createNewWebSocket(channel, new BinanceApiWebSocketListener<>(callback, new TypeReference<List<AllMarketTickersEvent>>() {}));
6864
}
6965

66+
@Override
67+
public Closeable onBookTickerEvent(String symbols, BinanceApiCallback<BookTickerEvent> callback) {
68+
final String channel = Arrays.stream(symbols.split(","))
69+
.map(String::trim)
70+
.map(s -> String.format("%s@bookTicker", s))
71+
.collect(Collectors.joining("/"));
72+
return createNewWebSocket(channel, new BinanceApiWebSocketListener<>(callback, BookTickerEvent.class));
73+
}
74+
7075
/**
7176
* @deprecated This method is no longer functional. Please use the returned {@link Closeable} from any of the other methods to close the web socket.
7277
*/

0 commit comments

Comments
 (0)