Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/main/java/com/kucoin/sdk/KucoinClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.kucoin.sdk.rest.adapter.*;
import com.kucoin.sdk.rest.interfaces.*;
import com.kucoin.sdk.websocket.KucoinAPICallback;
import org.apache.commons.lang3.StringUtils;

import com.kucoin.sdk.constants.APIConstants;
Expand Down Expand Up @@ -69,6 +70,9 @@ public class KucoinClientBuilder {

private LoanAPI loanAPI;

private KucoinAPICallback connectedCallback;
private KucoinAPICallback disConnectedCallback;

public KucoinRestClient buildRestClient() {
if (StringUtils.isBlank(baseUrl)) baseUrl = APIConstants.API_BASE_URL;
if (userAPI == null) userAPI = new UserAPIAdapter(baseUrl, apiKey, secret, passPhrase, apiKeyVersion);
Expand All @@ -92,7 +96,12 @@ public KucoinRestClient buildRestClient() {
public KucoinPublicWSClient buildPublicWSClient() throws IOException {
if (StringUtils.isBlank(baseUrl)) baseUrl = APIConstants.API_BASE_URL;
if (chooseServerStrategy == null) chooseServerStrategy = new RandomChooseStrategy();
KucoinPublicWSClientImpl client = new KucoinPublicWSClientImpl(this);
KucoinPublicWSClientImpl client;
if (connectedCallback == null) {
client = new KucoinPublicWSClientImpl(this);
} else {
client = new KucoinPublicWSClientImpl(this, connectedCallback, disConnectedCallback);
}
client.connect();
return client;
}
Expand Down Expand Up @@ -181,4 +190,12 @@ public KucoinClientBuilder withChooseServerStrategy(ChooseServerStrategy chooseS
this.chooseServerStrategy = chooseServerStrategy;
return this;
}

public KucoinClientBuilder withCallback(KucoinAPICallback connectedCallback,
KucoinAPICallback disconnectedCallback) {
this.connectedCallback = connectedCallback;
this.disConnectedCallback = disconnectedCallback;
return this;
}

}
5 changes: 4 additions & 1 deletion src/main/java/com/kucoin/sdk/factory/HttpClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;

import java.util.concurrent.TimeUnit;

/**
* Created by chenshiwei on 2019/1/18.
*/
Expand All @@ -26,7 +28,8 @@ private static OkHttpClient buildHttpClient(Interceptor interceptor) {
dispatcher.setMaxRequestsPerHost(100);
dispatcher.setMaxRequests(100);
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.dispatcher(dispatcher);
.dispatcher(dispatcher)
.pingInterval(20, TimeUnit.SECONDS);
if (interceptor != null) {
builder.addInterceptor(interceptor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ public KucoinPublicWSClientImpl(KucoinClientBuilder kucoinClientBuilder) {
new WebsocketPublicAPIAdaptor(kucoinClientBuilder.getBaseUrl()));
}

public KucoinPublicWSClientImpl(KucoinClientBuilder kucoinClientBuilder,
KucoinAPICallback connectedCallback, KucoinAPICallback disconnectedCallback) {
this(
HttpClientFactory.getPublicClient(),
new KucoinPublicWebsocketListener(connectedCallback, disconnectedCallback),
kucoinClientBuilder.getChooseServerStrategy(),
new WebsocketPublicAPIAdaptor(kucoinClientBuilder.getBaseUrl()));
}

private KucoinPublicWSClientImpl(OkHttpClient client,
KucoinPublicWebsocketListener listener,
ChooseServerStrategy chooseServerStrategy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,12 @@ public String getPassPhrase() {
public void setPassPhrase(String passPhrase) {
this.passPhrase = passPhrase;
}

public Integer getApiKeyVersion() {
return apiKeyVersion;
}

public void setApiKeyVersion(Integer apiKeyVersion) {
this.apiKeyVersion = apiKeyVersion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ public class AccountChangeEvent {

private String relationEvent;

private String relationContext;
private RelationContext relationContext;

private String relationEventId;

private String time;

private String accountId;

@Data
public static class RelationContext {

private String symbol;

private String orderId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,21 @@ public class KucoinPublicWebsocketListener extends WebSocketListener {
private KucoinAPICallback<KucoinEvent<Level3Event>> level3V2Callback = new PrintCallback<>();
private KucoinAPICallback<KucoinEvent<SnapshotEvent>> snapshotCallback = new PrintCallback<>();

private KucoinAPICallback<String> connectedCallback;
private KucoinAPICallback<String> disConnectedCallback;

public KucoinPublicWebsocketListener() {}

public KucoinPublicWebsocketListener(KucoinAPICallback<String> connectedCallback,
KucoinAPICallback<String> disConnectedCallback) {
this.connectedCallback = connectedCallback;
this.disConnectedCallback = disConnectedCallback;
}

@Override
public void onOpen(WebSocket webSocket, Response response) {
LOGGER.debug("web socket open");
this.connectedCallback.onResponse(response.message());
}

@Override
Expand Down Expand Up @@ -94,7 +106,18 @@ 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);
this.disConnectedCallback.onResponse(response.message());
}

@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
this.disConnectedCallback.onResponse(reason);
}

@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
this.disConnectedCallback.onResponse(reason);
}

private JsonNode tree(String text) {
Expand Down