Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature proposal: client side websocket ping #13

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.binance.connector.futures.client.utils.WebSocketCallback;
import com.binance.connector.futures.client.utils.ParameterChecker;
import okhttp3.Request;
import java.time.Duration;

/**
* <h2>COIN-M Websocket Streams</h2>
Expand All @@ -17,11 +18,15 @@
*/
public class CMWebsocketClientImpl extends WebsocketClientImpl {
public CMWebsocketClientImpl() {
super(DefaultUrls.COINM_WS_URL);
super(DefaultUrls.COINM_WS_URL, Duration.ZERO);
}

public CMWebsocketClientImpl(String baseUrl) {
super(baseUrl);
super(baseUrl, Duration.ZERO);
}

public CMWebsocketClientImpl(String baseUrl, Duration pingInterval) {
super(baseUrl, pingInterval);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.binance.connector.futures.client.utils.WebSocketCallback;
import com.binance.connector.futures.client.utils.ParameterChecker;
import okhttp3.Request;
import java.time.Duration;

/**
* <h2>USDⓈ-M Websocket Streams</h2>
Expand All @@ -18,11 +19,15 @@
public class UMWebsocketClientImpl extends WebsocketClientImpl {

public UMWebsocketClientImpl() {
super(DefaultUrls.USDM_WS_URL);
super(DefaultUrls.USDM_WS_URL, Duration.ZERO);
}

public UMWebsocketClientImpl(String baseUrl) {
super(baseUrl);
super(baseUrl, Duration.ZERO);
}

public UMWebsocketClientImpl(String baseUrl, Duration pingInterval) {
super(baseUrl, pingInterval);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -28,13 +29,15 @@
*/
public abstract class WebsocketClientImpl implements WebsocketClient {
private final String baseUrl;
private final Duration pingInterval;
private final Map<Integer, WebSocketConnection> connections = new HashMap<>();
private final WebSocketCallback noopCallback = msg -> {
};
private static final Logger logger = LoggerFactory.getLogger(WebsocketClientImpl.class);

public WebsocketClientImpl(String baseUrl) {
public WebsocketClientImpl(String baseUrl, Duration pingInterval) {
this.baseUrl = baseUrl;
this.pingInterval = pingInterval;
}

public WebSocketCallback getNoopCallback() {
Expand Down Expand Up @@ -715,7 +718,7 @@ public int createConnection(
WebSocketCallback onFailureCallback,
Request request
) {
WebSocketConnection connection = new WebSocketConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request);
WebSocketConnection connection = new WebSocketConnection(onOpenCallback, onMessageCallback, onClosingCallback, onFailureCallback, request, this.pingInterval);
connection.connect();
int connectionId = connection.getConnectionId();
connections.put(connectionId, connection);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.binance.connector.futures.client.utils;

import java.time.Duration;
import java.util.concurrent.atomic.AtomicInteger;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand All @@ -12,7 +13,6 @@
public class WebSocketConnection extends WebSocketListener {
private static final AtomicInteger connectionCounter = new AtomicInteger(0);
private static final int NORMAL_CLOSURE_STATUS = 1000;
private static final OkHttpClient client = HttpClientSingleton.getHttpClient();
private static final Logger logger = LoggerFactory.getLogger(WebSocketConnection.class);

private final WebSocketCallback onOpenCallback;
Expand All @@ -23,6 +23,7 @@ public class WebSocketConnection extends WebSocketListener {
private final Request request;
private final String streamName;

private final OkHttpClient client;
private WebSocket webSocket;

private final Object mutex;
Expand All @@ -32,7 +33,8 @@ public WebSocketConnection(
WebSocketCallback onMessageCallback,
WebSocketCallback onClosingCallback,
WebSocketCallback onFailureCallback,
Request request
Request request,
Duration pingInterval
) {
this.onOpenCallback = onOpenCallback;
this.onMessageCallback = onMessageCallback;
Expand All @@ -43,6 +45,10 @@ public WebSocketConnection(
this.streamName = request.url().host() + request.url().encodedPath();
this.webSocket = null;
this.mutex = new Object();

OkHttpClient.Builder builder = HttpClientSingleton.getHttpClient().newBuilder();
builder.pingInterval(pingInterval);
this.client = builder.build();
}

public void connect() {
Expand Down