diff --git a/README.md b/README.md index 811850d..366639b 100644 --- a/README.md +++ b/README.md @@ -133,32 +133,112 @@ wsClient.close(); ##### Listen for changes to the order boock for ETH-BTC and KCS-BTC ```java -kucoinPublicWSClient.onTicker(response -> { - System.out.println(response); - }, "ETH-BTC", "KCS-BTC"); -kucoinPublicWSClient.onLevel2Data(response -> { - System.out.println(response); - }, "ETH-BTC", "KCS-BTC"); -kucoinPublicWSClient.onMatchExecutionData(response -> { - System.out.println(response); - }); -kucoinPublicWSClient.onLevel3Data(response -> { - System.out.println(response); - }, "ETH-BTC", "KCS-BTC"); +kucoinPublicWSClient.onTicker(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + System.out.println(response); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + //reinitializeWSConnection(); //implement this method + } + +}, "ETH-BTC", "KCS-BTC"); + +kucoinPublicWSClient.onLevel2Data(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + System.out.println(response); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + //reinitializeWSConnection(); //implement this method + } + +}, "ETH-BTC", "KCS-BTC"); + + +kucoinPublicWSClient.onMatchExecutionData(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + System.out.println(response); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + //reinitializeWSConnection(); //implement this method + } + +}, "ETH-BTC", "KCS-BTC"); + + +kucoinPublicWSClient.onLevel3Data(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + System.out.println(response); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + //reinitializeWSConnection(); //implement this method + } + +}, "ETH-BTC", "KCS-BTC"); + ``` + #### Private Channels ##### Listen for account balance changes + ```java -kucoinPrivateWSClient.onAccountBalance(response -> { - System.out.println(response); - }); + +kucoinPrivateWSClient.onAccountBalance(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + System.out.println(response); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + //reinitializeWSConnection(); //implement this method + } + +}); + ``` + ##### Listen for order activate message + ```java -kucoinPrivateWSClient.onOrderActivate(response -> { - System.out.println(response); - }, "ETH-BTC", "KCS-BTC"); + +kucoinPrivateWSClient.onOrderActivate(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + System.out.println(response); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + //reinitializeWSConnection(); //implement this method + } + +}, "ETH-BTC", "KCS-BTC"); + ``` ## Testing diff --git a/src/main/java/com/kucoin/sdk/KucoinClientBuilder.java b/src/main/java/com/kucoin/sdk/KucoinClientBuilder.java index 1d13fd0..176b0af 100644 --- a/src/main/java/com/kucoin/sdk/KucoinClientBuilder.java +++ b/src/main/java/com/kucoin/sdk/KucoinClientBuilder.java @@ -84,7 +84,7 @@ public KucoinRestClient buildRestClient() { if (timeAPI == null) timeAPI = new TimeAPIAdapter(baseUrl); if (commonAPI == null) commonAPI = new CommonAPIAdapter(baseUrl); if (symbolAPI == null) symbolAPI = new SymbolAPIAdaptor(baseUrl); - if (orderBookAPI == null) orderBookAPI = new OrderBookAPIAdapter(baseUrl); + if (orderBookAPI == null) orderBookAPI = new OrderBookAPIAdapter(baseUrl, apiKey, secret, passPhrase, apiKeyVersion); if (historyAPI == null) historyAPI = new HistoryAPIAdapter(baseUrl); return new KucoinRestClientImpl(this); } diff --git a/src/main/java/com/kucoin/sdk/factory/HttpClientFactory.java b/src/main/java/com/kucoin/sdk/factory/HttpClientFactory.java index 5ce529c..4a6415c 100644 --- a/src/main/java/com/kucoin/sdk/factory/HttpClientFactory.java +++ b/src/main/java/com/kucoin/sdk/factory/HttpClientFactory.java @@ -3,6 +3,8 @@ */ package com.kucoin.sdk.factory; +import java.util.concurrent.TimeUnit; + import com.kucoin.sdk.rest.interceptor.AuthenticationInterceptor; import okhttp3.Dispatcher; import okhttp3.Interceptor; @@ -26,7 +28,7 @@ private static OkHttpClient buildHttpClient(Interceptor interceptor) { dispatcher.setMaxRequestsPerHost(100); dispatcher.setMaxRequests(100); OkHttpClient.Builder builder = new OkHttpClient.Builder() - .dispatcher(dispatcher); + .dispatcher(dispatcher).pingInterval(30, TimeUnit.SECONDS); if (interceptor != null) { builder.addInterceptor(interceptor); } diff --git a/src/main/java/com/kucoin/sdk/rest/adapter/OrderBookAPIAdapter.java b/src/main/java/com/kucoin/sdk/rest/adapter/OrderBookAPIAdapter.java index 0ee08be..0a386f9 100644 --- a/src/main/java/com/kucoin/sdk/rest/adapter/OrderBookAPIAdapter.java +++ b/src/main/java/com/kucoin/sdk/rest/adapter/OrderBookAPIAdapter.java @@ -3,7 +3,7 @@ */ package com.kucoin.sdk.rest.adapter; -import com.kucoin.sdk.rest.impl.retrofit.PublicRetrofitAPIImpl; +import com.kucoin.sdk.rest.impl.retrofit.AuthRetrofitAPIImpl; import com.kucoin.sdk.rest.interfaces.OrderBookAPI; import com.kucoin.sdk.rest.interfaces.retrofit.OrderBookAPIRetrofit; import com.kucoin.sdk.rest.response.Level3Response; @@ -14,10 +14,14 @@ /** * Created by chenshiwei on 2019/1/22. */ -public class OrderBookAPIAdapter extends PublicRetrofitAPIImpl implements OrderBookAPI { +public class OrderBookAPIAdapter extends AuthRetrofitAPIImpl implements OrderBookAPI { - public OrderBookAPIAdapter(String baseUrl) { + public OrderBookAPIAdapter(String baseUrl, String apiKey, String secret, String passPhrase, Integer apiKeyVersion) { this.baseUrl = baseUrl; + this.apiKey = apiKey; + this.secret = secret; + this.passPhrase = passPhrase; + this.apiKeyVersion = apiKeyVersion; } @Override diff --git a/src/main/java/com/kucoin/sdk/rest/interfaces/retrofit/OrderBookAPIRetrofit.java b/src/main/java/com/kucoin/sdk/rest/interfaces/retrofit/OrderBookAPIRetrofit.java index 063ab35..d54169e 100644 --- a/src/main/java/com/kucoin/sdk/rest/interfaces/retrofit/OrderBookAPIRetrofit.java +++ b/src/main/java/com/kucoin/sdk/rest/interfaces/retrofit/OrderBookAPIRetrofit.java @@ -21,7 +21,7 @@ public interface OrderBookAPIRetrofit { @GET("api/v1/market/orderbook/level2_20") Call> getTop20Level2OrderBook(@Query("symbol") String symbol); - @GET("api/v2/market/orderbook/level2") + @GET("api/v3/market/orderbook/level2") Call> getFullLevel2OrderBook(@Query("symbol") String symbol); @GET("api/v2/market/orderbook/level3") diff --git a/src/main/java/com/kucoin/sdk/websocket/KucoinAPICallback.java b/src/main/java/com/kucoin/sdk/websocket/KucoinAPICallback.java index 99bdbc1..04c58ce 100644 --- a/src/main/java/com/kucoin/sdk/websocket/KucoinAPICallback.java +++ b/src/main/java/com/kucoin/sdk/websocket/KucoinAPICallback.java @@ -11,5 +11,7 @@ public interface KucoinAPICallback { void onResponse(T response) throws KucoinApiException; + + void onFailure(Throwable cause); } diff --git a/src/main/java/com/kucoin/sdk/websocket/PrintCallback.java b/src/main/java/com/kucoin/sdk/websocket/PrintCallback.java index debe346..7bad68f 100644 --- a/src/main/java/com/kucoin/sdk/websocket/PrintCallback.java +++ b/src/main/java/com/kucoin/sdk/websocket/PrintCallback.java @@ -20,4 +20,9 @@ public void onResponse(T response) throws KucoinApiException { LOGGER.debug("Got response: {}", response); } + @Override + public void onFailure(Throwable cause) { + LOGGER.debug("Got exception: {}", cause); + } + } diff --git a/src/main/java/com/kucoin/sdk/websocket/listener/KucoinPrivateWebsocketListener.java b/src/main/java/com/kucoin/sdk/websocket/listener/KucoinPrivateWebsocketListener.java index 9c12fa2..8fe7913 100644 --- a/src/main/java/com/kucoin/sdk/websocket/listener/KucoinPrivateWebsocketListener.java +++ b/src/main/java/com/kucoin/sdk/websocket/listener/KucoinPrivateWebsocketListener.java @@ -81,6 +81,19 @@ public void onMessage(WebSocket webSocket, String text) { @Override public void onFailure(WebSocket webSocket, Throwable t, Response response) { LOGGER.error("Error on private socket", t); + + if( orderActivateCallback != null ) { + orderActivateCallback.onFailure(t); + } + if( accountChangeCallback != null ) { + accountChangeCallback.onFailure(t); + } + if( orderChangeCallback != null ) { + orderChangeCallback.onFailure(t); + } + if( advancedOrderCallback != null ) { + advancedOrderCallback.onFailure(t); + } } private JsonNode tree(String text) { diff --git a/src/main/java/com/kucoin/sdk/websocket/listener/KucoinPublicWebsocketListener.java b/src/main/java/com/kucoin/sdk/websocket/listener/KucoinPublicWebsocketListener.java index 13c26c4..0f62839 100644 --- a/src/main/java/com/kucoin/sdk/websocket/listener/KucoinPublicWebsocketListener.java +++ b/src/main/java/com/kucoin/sdk/websocket/listener/KucoinPublicWebsocketListener.java @@ -94,7 +94,32 @@ public void onMessage(WebSocket webSocket, String text) { @Override public void onFailure(WebSocket webSocket, Throwable t, Response response) { - LOGGER.error("Error on private socket", t); + LOGGER.error("Error on public socket", t); + + if( tickerCallback != null ) { + tickerCallback.onFailure(t); + } + if( level2Callback != null ) { + level2Callback.onFailure(t); + } + if( level2Depth5Callback != null ) { + level2Depth5Callback.onFailure(t); + } + if( level2Depth50Callback != null ) { + level2Depth50Callback.onFailure(t); + } + if( matchDataCallback != null ) { + matchDataCallback.onFailure(t); + } + if( level3Callback != null ) { + level3Callback.onFailure(t); + } + if( level3V2Callback != null ) { + level3V2Callback.onFailure(t); + } + if( snapshotCallback != null ) { + snapshotCallback.onFailure(t); + } } private JsonNode tree(String text) { diff --git a/src/test/java/com/kucoin/sdk/KucoinPrivateWSClientTest.java b/src/test/java/com/kucoin/sdk/KucoinPrivateWSClientTest.java index 76f6d4f..adf2c06 100644 --- a/src/test/java/com/kucoin/sdk/KucoinPrivateWSClientTest.java +++ b/src/test/java/com/kucoin/sdk/KucoinPrivateWSClientTest.java @@ -3,6 +3,7 @@ */ package com.kucoin.sdk; +import com.kucoin.sdk.exception.KucoinApiException; import com.kucoin.sdk.model.enums.ApiKeyVersionEnum; import com.kucoin.sdk.model.enums.PrivateChannelEnum; import com.kucoin.sdk.rest.request.AccountTransferV2Request; @@ -10,8 +11,10 @@ import com.kucoin.sdk.rest.request.StopOrderCreateRequest; import com.kucoin.sdk.rest.response.AccountBalancesResponse; import com.kucoin.sdk.rest.response.OrderCreateResponse; +import com.kucoin.sdk.websocket.KucoinAPICallback; import com.kucoin.sdk.websocket.event.AccountChangeEvent; import com.kucoin.sdk.websocket.event.AdvancedOrderEvent; +import com.kucoin.sdk.websocket.event.KucoinEvent; import com.kucoin.sdk.websocket.event.OrderActivateEvent; import com.kucoin.sdk.websocket.event.OrderChangeEvent; import org.hamcrest.core.Is; @@ -64,12 +67,33 @@ public void onOrderActivate() throws Exception { AtomicReference event = new AtomicReference<>(); CountDownLatch gotEvent = new CountDownLatch(1); + kucoinPrivateWSClient.onOrderActivate(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + LOGGER.info("Got response"); + event.set(response.getData()); + kucoinPrivateWSClient.unsubscribe(PrivateChannelEnum.ORDER, "ETH-BTC", "KCS-BTC"); + gotEvent.countDown(); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + + //reinitializeWSConnection(); //implement this method + } + + }, "ETH-BTC", "KCS-BTC"); + + /* kucoinPrivateWSClient.onOrderActivate(response -> { LOGGER.info("Got response"); event.set(response.getData()); kucoinPrivateWSClient.unsubscribe(PrivateChannelEnum.ORDER, "ETH-BTC", "KCS-BTC"); gotEvent.countDown(); }, "ETH-BTC", "KCS-BTC"); + */ Thread.sleep(1000); @@ -93,12 +117,33 @@ public void onOrderChange() throws Exception { AtomicReference event = new AtomicReference<>(); CountDownLatch gotEvent = new CountDownLatch(1); + kucoinPrivateWSClient.onOrderChange(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + LOGGER.info("Got response"); + event.set(response.getData()); + kucoinPrivateWSClient.unsubscribe(PrivateChannelEnum.ORDER_CHANGE, "ETH-BTC", "BTC-USDT"); + gotEvent.countDown(); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + + //reinitializeWSConnection(); //implement this method + } + + }, "ETH-BTC", "BTC-USDT"); + + /* kucoinPrivateWSClient.onOrderChange(response -> { LOGGER.info("Got response"); event.set(response.getData()); kucoinPrivateWSClient.unsubscribe(PrivateChannelEnum.ORDER_CHANGE, "ETH-BTC", "BTC-USDT"); gotEvent.countDown(); }, "ETH-BTC", "BTC-USDT"); + */ Thread.sleep(1000); @@ -122,12 +167,33 @@ public void onAccountBalance() throws Exception { AtomicReference event = new AtomicReference<>(); CountDownLatch gotEvent = new CountDownLatch(1); + kucoinPrivateWSClient.onAccountBalance(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + LOGGER.info("Got response"); + event.set(response.getData()); + kucoinPrivateWSClient.unsubscribe(PrivateChannelEnum.ACCOUNT); + gotEvent.countDown(); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + + //reinitializeWSConnection(); //implement this method + } + + }); + + /* kucoinPrivateWSClient.onAccountBalance(response -> { LOGGER.info("Got response"); event.set(response.getData()); kucoinPrivateWSClient.unsubscribe(PrivateChannelEnum.ACCOUNT); gotEvent.countDown(); }); + */ Thread.sleep(1000); @@ -158,12 +224,33 @@ public void onAdvancedOrder() throws Exception { AtomicReference event = new AtomicReference<>(); CountDownLatch gotEvent = new CountDownLatch(1); + kucoinPrivateWSClient.onAdvancedOrder(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + LOGGER.info("Got response {}", response); + event.set(response.getData()); + kucoinPrivateWSClient.unsubscribe(PrivateChannelEnum.ADVANCED_ORDER, "ETH-BTC"); + gotEvent.countDown(); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + + //reinitializeWSConnection(); //implement this method + } + + }, "ETH-BTC"); + + /* kucoinPrivateWSClient.onAdvancedOrder(response -> { LOGGER.info("Got response {}", response); event.set(response.getData()); kucoinPrivateWSClient.unsubscribe(PrivateChannelEnum.ADVANCED_ORDER, "ETH-BTC"); gotEvent.countDown(); }, "ETH-BTC"); + */ Thread.sleep(1000); diff --git a/src/test/java/com/kucoin/sdk/KucoinPublicWSClientTest.java b/src/test/java/com/kucoin/sdk/KucoinPublicWSClientTest.java index 4efffcc..493bb0d 100644 --- a/src/test/java/com/kucoin/sdk/KucoinPublicWSClientTest.java +++ b/src/test/java/com/kucoin/sdk/KucoinPublicWSClientTest.java @@ -3,11 +3,13 @@ */ package com.kucoin.sdk; +import com.kucoin.sdk.exception.KucoinApiException; import com.kucoin.sdk.model.enums.ApiKeyVersionEnum; import com.kucoin.sdk.model.enums.PublicChannelEnum; import com.kucoin.sdk.rest.request.OrderCreateApiRequest; import com.kucoin.sdk.rest.response.OrderCreateResponse; import com.kucoin.sdk.rest.response.TickerResponse; +import com.kucoin.sdk.websocket.KucoinAPICallback; import com.kucoin.sdk.websocket.event.*; import org.hamcrest.core.Is; import org.junit.AfterClass; @@ -56,11 +58,31 @@ public void onTicker() throws Exception { AtomicReference event = new AtomicReference<>(); CountDownLatch gotEvent = new CountDownLatch(1); + kucoinPublicWSClient.onTicker(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + event.set(response.getData()); + kucoinPublicWSClient.unsubscribe(PublicChannelEnum.TICKER, "ETH-BTC", "KCS-BTC"); + gotEvent.countDown(); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + + //reinitializeWSConnection(); //implement this method + } + + }, "ETH-BTC", "KCS-BTC"); + + /* kucoinPublicWSClient.onTicker(response -> { event.set(response.getData()); kucoinPublicWSClient.unsubscribe(PublicChannelEnum.TICKER, "ETH-BTC", "KCS-BTC"); gotEvent.countDown(); }, "ETH-BTC", "KCS-BTC"); + */ // Make some actual executions buyAndSell(); @@ -74,11 +96,31 @@ public void onLevel2Data() throws Exception { AtomicReference event = new AtomicReference<>(); CountDownLatch gotEvent = new CountDownLatch(1); + kucoinPublicWSClient.onLevel2Data(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + event.set(response.getData()); + kucoinPublicWSClient.unsubscribe(PublicChannelEnum.LEVEL2, "ETH-BTC", "KCS-BTC"); + gotEvent.countDown(); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + + //reinitializeWSConnection(); //implement this method + } + + }, "ETH-BTC", "KCS-BTC"); + + /* kucoinPublicWSClient.onLevel2Data(response -> { event.set(response.getData()); kucoinPublicWSClient.unsubscribe(PublicChannelEnum.LEVEL2, "ETH-BTC", "KCS-BTC"); gotEvent.countDown(); }, "ETH-BTC", "KCS-BTC"); + */ // Trigger a market change placeOrderAndCancelOrder(); @@ -92,11 +134,31 @@ public void onLevel2Depth5Data() throws Exception { AtomicReference event = new AtomicReference<>(); CountDownLatch gotEvent = new CountDownLatch(1); + kucoinPublicWSClient.onLevel2Data(5, new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + event.set(response.getData()); + kucoinPublicWSClient.unsubscribe(PublicChannelEnum.LEVEL2_DEPTH5, "ETH-BTC", "BTC-USDT"); + gotEvent.countDown(); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + + //reinitializeWSConnection(); //implement this method + } + + }, "ETH-BTC", "BTC-USDT"); + + /* kucoinPublicWSClient.onLevel2Data(5, response -> { event.set(response.getData()); kucoinPublicWSClient.unsubscribe(PublicChannelEnum.LEVEL2_DEPTH5, "ETH-BTC", "BTC-USDT"); gotEvent.countDown(); }, "ETH-BTC", "BTC-USDT"); + */ // Trigger a market change placeOrderAndCancelOrder(); @@ -110,11 +172,30 @@ public void onLevel2Depth50Data() throws Exception { AtomicReference event = new AtomicReference<>(); CountDownLatch gotEvent = new CountDownLatch(1); + kucoinPublicWSClient.onLevel2Data(50, new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + event.set(response.getData()); + kucoinPublicWSClient.unsubscribe(PublicChannelEnum.LEVEL2_DEPTH50, "ETH-BTC", "BTC-USDT"); + gotEvent.countDown(); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + + //reinitializeWSConnection(); //implement this method + } + }, "ETH-BTC", "BTC-USDT"); + + /* kucoinPublicWSClient.onLevel2Data(50, response -> { event.set(response.getData()); kucoinPublicWSClient.unsubscribe(PublicChannelEnum.LEVEL2_DEPTH50, "ETH-BTC", "BTC-USDT"); gotEvent.countDown(); }, "ETH-BTC", "BTC-USDT"); + */ // Trigger a market change placeOrderAndCancelOrder(); @@ -128,11 +209,31 @@ public void onMatchExecutionData() throws Exception { AtomicReference event = new AtomicReference<>(); CountDownLatch gotEvent = new CountDownLatch(1); + kucoinPublicWSClient.onMatchExecutionData(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + event.set(response.getData()); + kucoinPublicWSClient.unsubscribe(PublicChannelEnum.MATCH, "ETH-BTC", "KCS-BTC"); + gotEvent.countDown(); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + + //reinitializeWSConnection(); //implement this method + } + + }, "ETH-BTC", "KCS-BTC"); + + /* kucoinPublicWSClient.onMatchExecutionData(response -> { event.set(response.getData()); kucoinPublicWSClient.unsubscribe(PublicChannelEnum.MATCH, "ETH-BTC", "KCS-BTC"); gotEvent.countDown(); }, "ETH-BTC", "KCS-BTC"); + */ // Make some actual executions buyAndSell(); @@ -146,11 +247,31 @@ public void onLevel3Data() throws Exception { AtomicReference event = new AtomicReference<>(); CountDownLatch gotEvent = new CountDownLatch(1); + kucoinPublicWSClient.onLevel3Data(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + event.set(response.getData()); + kucoinPublicWSClient.unsubscribe(PublicChannelEnum.LEVEL3, "ETH-BTC", "KCS-BTC"); + gotEvent.countDown(); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + + //reinitializeWSConnection(); //implement this method + } + + }, "ETH-BTC", "KCS-BTC"); + + /* kucoinPublicWSClient.onLevel3Data(response -> { event.set(response.getData()); kucoinPublicWSClient.unsubscribe(PublicChannelEnum.LEVEL3, "ETH-BTC", "KCS-BTC"); gotEvent.countDown(); }, "ETH-BTC", "KCS-BTC"); + */ // Trigger a market change placeOrderAndCancelOrder(); @@ -164,11 +285,31 @@ public void onLevel3_V2Data() throws Exception { AtomicReference event = new AtomicReference<>(); CountDownLatch gotEvent = new CountDownLatch(1); + kucoinPublicWSClient.onLevel3Data_V2(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + event.set(response.getData()); + kucoinPublicWSClient.unsubscribe(PublicChannelEnum.LEVEL3_V2, "ETH-BTC", "BTC-USDT"); + gotEvent.countDown(); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + + //reinitializeWSConnection(); //implement this method + } + + }, "ETH-BTC", "BTC-USDT"); + + /* kucoinPublicWSClient.onLevel3Data_V2(response -> { event.set(response.getData()); kucoinPublicWSClient.unsubscribe(PublicChannelEnum.LEVEL3_V2, "ETH-BTC", "BTC-USDT"); gotEvent.countDown(); }, "ETH-BTC", "BTC-USDT"); + */ // Trigger a market change placeOrderAndCancelOrder(); @@ -188,11 +329,33 @@ public void ping() throws Exception { public void onSnapshot() throws Exception { AtomicReference event = new AtomicReference<>(); CountDownLatch gotEvent = new CountDownLatch(1); + + kucoinPublicWSClient.onSnapshot(new KucoinAPICallback>() { + + @Override + public void onResponse(KucoinEvent response) throws KucoinApiException { + event.set(response.getData()); + kucoinPublicWSClient.unsubscribe(PublicChannelEnum.SNAPSHOT, "ETH-BTC"); + gotEvent.countDown(); + } + + @Override + public void onFailure(Throwable cause) { + System.out.println("WS connection failed. Reconnecting. cause:" + cause.getMessage()); + + //reinitializeWSConnection(); //implement this method + } + + }, "ETH-BTC"); + + /* kucoinPublicWSClient.onSnapshot(response -> { event.set(response.getData()); kucoinPublicWSClient.unsubscribe(PublicChannelEnum.SNAPSHOT, "ETH-BTC"); gotEvent.countDown(); }, "ETH-BTC"); + */ + placeOrderAndCancelOrder(); gotEvent.await(20, TimeUnit.SECONDS); System.out.println(event.get());