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

feat(websocket): support proxy #110

Open
wants to merge 1 commit into
base: master
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
@@ -1,5 +1,6 @@
package com.binance.connector.client;

import com.binance.connector.client.utils.ProxyAuth;
import java.util.ArrayList;

import com.binance.connector.client.utils.websocketcallback.WebSocketClosedCallback;
Expand Down Expand Up @@ -39,4 +40,5 @@ public interface WebSocketStreamClient {
int combineStreams(ArrayList<String> streams, WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback);
void closeConnection(int streamId);
void closeAllConnections();
void setProxy(ProxyAuth proxy);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.binance.connector.client.impl;

import com.binance.connector.client.utils.ProxyAuth;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -36,7 +37,7 @@
*/
public class WebSocketStreamClientImpl implements WebSocketStreamClient {
private static final Logger logger = LoggerFactory.getLogger(WebSocketStreamClientImpl.class);
private static final OkHttpClient client = WebSocketStreamHttpClientSingleton.getHttpClient();
private static OkHttpClient client = WebSocketStreamHttpClientSingleton.getHttpClient();
private final String baseUrl;
private final Map<Integer, WebSocketConnection> connections = new HashMap<>();
private final WebSocketOpenCallback noopOpenCallback = response -> { };
Expand Down Expand Up @@ -600,6 +601,11 @@ public void closeAllConnections() {
}
}

@Override
public void setProxy(ProxyAuth proxy) {
client = WebSocketStreamHttpClientSingleton.getHttpClientWithProxy(proxy);
}

private int createConnection(
WebSocketOpenCallback onOpenCallback,
WebSocketMessageCallback onMessageCallback,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
package com.binance.connector.client.utils.httpclient;

import com.binance.connector.client.utils.ProxyAuth;
import okhttp3.OkHttpClient;

public final class WebSocketStreamHttpClientSingleton {
private static final OkHttpClient httpClient = new OkHttpClient();

private WebSocketStreamHttpClientSingleton() {
private static OkHttpClient httpClient = null;

private WebSocketStreamHttpClientSingleton() {
}

public static OkHttpClient getHttpClient() {
if (httpClient == null) {
httpClient = new OkHttpClient();
}
return httpClient;
}

public static OkHttpClient getHttpClientWithProxy(ProxyAuth proxy) {
createClientWithProxy(proxy);
return httpClient;
}

public static OkHttpClient getHttpClient() {
return httpClient;
public static void createClientWithProxy(ProxyAuth proxy) {
if (proxy == null) {
httpClient = new OkHttpClient();
} else {
if (proxy.getAuth() == null) {
httpClient = new OkHttpClient.Builder().proxy(proxy.getProxy()).build();
} else {
httpClient = new OkHttpClient.Builder().proxy(proxy.getProxy())
.proxyAuthenticator(proxy.getAuth()).build();
}
}
}

}