我正试图使用TooTallNate的Java Websocket库来创建一个接收来自coinbase交换Websocket流的消息的Websocket客户端。由于Python中的并行化瓶颈,我正在将我用Python制作的程序移植到Java中,据我所知,我在Java中做的事情与在Python中做的一样。以下是我使用这个websocketlib在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作为URI而不是wss://ws-feed.exchange.coinbase.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());
在执行connect()
之前,需要调用setSocket()
方法,例如:
private CoinbaseWebsocketClient(URI serverURI) {
super(serverURI, new Draft_17());
setSocket(SSLSocketFactory.getDefault().createSocket(serverURI.getHost(), serverURI.getPort()));
connect();
}
祝你好运!
注意:要注意,在出现任何故障的情况下,您的单例get()
可能不是一个好主意(如果我没记错的话,客户端类在出现不可恢复的错误后不可用,您必须创建一个新的客户端来恢复)。
vlp几乎是对的。但是硬编码443是否应作为端口参数:
private CoinbaseWebsocketClient(URI serverURI) {
super(serverURI, new Draft_17());
setSocket(SSLSocketFactory.getDefault().createSocket(serverURI.getHost(), 443));
connect();
}
这对我有用!
PS。ssl必须是有效的证书,否则不能仅使用getDefault方法来获取ssl上下文。对于自烧毛等,请参阅以下链接:
http://blog.antoine.li/2010/10/22/android-trusting-ssl-certificates/