gpt4 book ai didi

java - 使用 java-websocket 库连接到 coinbase exchange websocket 流时,Draft 拒绝握手

转载 作者:太空狗 更新时间:2023-10-29 22:58:34 25 4
gpt4 key购买 nike

我正在尝试使用 Java-Websocket library by TooTallNate创建一个从 coinbase exchange websocket stream 接收消息的 websocket 客户端.由于 Python 中的并行化瓶颈,我正在将我用 Python 编写的程序移植到 Java,据我所知,我在 Java 中做的事情与在 Python 中做的一样。这是我使用 this websocket lib 在 Python 中打开连接的代码(这按预期工作):

ws = websocket.create_connection("wss://ws-feed.exchange.coinbase.com", 20)
ws.send(json.dumps({
"type": "subscribe",
"product_id": "BTC-USD"
}))

这是我的整个 Java 类:

public class CoinbaseWebsocketClient extends WebSocketClient {

private final Gson gson = new Gson();

private CoinbaseWebsocketClient(URI serverURI) {
super(serverURI, new Draft_17());
connect();
}

private static URI uri;
private static CoinbaseWebsocketClient coinbaseWebsocketClient;

static {
try {
uri = new URI("wss://ws-feed.exchange.coinbase.com");
} catch (URISyntaxException e) {
e.printStackTrace();
}
}

protected static CoinbaseWebsocketClient get() {
if (coinbaseWebsocketClient == null) {
coinbaseWebsocketClient = new CoinbaseWebsocketClient(uri);
}
return coinbaseWebsocketClient;
}

@Override
public void onOpen(ServerHandshake serverHandshake) {
System.out.println("Websocket open");
final JsonObject btcUSD_Request = new JsonObject();
btcUSD_Request.addProperty("type", "subscribe");
btcUSD_Request.addProperty("product_id", "BTC_USD");
final String requestString = gson.toJson(btcUSD_Request);
send(requestString);
}

@Override
public void onMessage(String s) {
System.out.println("Message received: " + s);
}

@Override
public void onClose(int code, String reason, boolean remote) {
System.out.println("Websocket closed: " + reason);
}

@Override
public void onError(Exception e) {
System.err.println("an error occurred:" + e);
}

我知道我的 Java 代码没有完全根本性的问题,因为当我使用 ws://echo.websocket.org 而不是 wss://ws-feed.exchange.coinbase 作为 URI 时,它按预期工作.com。但是,当我尝试连接到 wss://ws-feed.exchange.coinbase.com 时,出现此错误:

Websocket closed: draft org.java_websocket.drafts.Draft_17@7ca2fefb refuses handshake

据我所知,这个连接没有身份验证或类似的东西(我没有在我的 Python 程序中提供任何东西)所以我不知道这个错误的来源是什么。

最佳答案

需要像下面这样创建 sslcontext。它跳过证书。我可以在没有证书的情况下成功建立连接

SSLContext sslContext = SSLContext.getInstance("SSL");

// set up a TrustManager that trusts everything
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
System.out.println("getAcceptedIssuers =============");
return null;
}

public void checkClientTrusted(X509Certificate[] certs,
String authType) {
System.out.println("checkClientTrusted =============");
}

public void checkServerTrusted(X509Certificate[] certs,
String authType) {
System.out.println("checkServerTrusted =============");
}
} }, new SecureRandom());

关于java - 使用 java-websocket 库连接到 coinbase exchange websocket 流时,Draft 拒绝握手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34627959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com