问题
I am using spring WebSocket WebSocketClient
to connect with GDAX server.
It is working fine with the Live environment, but the same code is not working with the sandbox environment.
Here is my code to connect to the server:
public class Test {
public static void main(String[] args) throws InterruptedException {
String socketURL = "wss://ws-feed.gdax.com"; //Live URL
//String socketURL = "wss://ws-feed-public.sandbox.gdax.com"; //sanbox URL, code will not work if you use sandvox URL
WebSocketClient client = new StandardWebSocketClient();
WebSocketConnectionManager connectionManager = new WebSocketConnectionManager(client, new MySocketHandler(), socketURL);
connectionManager.start();
Thread.sleep(1000000);
}
}
class MySocketHandler extends TextWebSocketHandler {
Logger LOG = LoggerFactory.getLogger(MySocketHandler.class);
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
LOG.info("Message Received [" + message.getPayload() + "]");
}
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
LOG.info("Connected");
String payload = "{\"type\": \"subscribe\",\"channels\":[{\"name\": \"heartbeat\",\"product_ids\": [\"BTC-USD\"]}]}";
LOG.info("Sending [" + payload + "]");
session.sendMessage(new TextMessage(payload));
}
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) {
LOG.error("Transport Error", exception);
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status){
LOG.info("Connection Closed [" + status.getReason() + "]");
}
}
Above code is working fine with the Live URL and output is:
15:52:10.940 [main] INFO org.springframework.web.socket.client.WebSocketConnectionManager - Starting WebSocketConnectionManager
15:52:10.950 [main] INFO org.springframework.web.socket.client.WebSocketConnectionManager - Connecting to WebSocket at wss://ws-feed.gdax.com
15:52:10.950 [main] DEBUG org.springframework.web.socket.client.standard.StandardWebSocketClient - Connecting to wss://ws-feed.gdax.com
15:52:12.741 [SimpleAsyncTaskExecutor-1] DEBUG org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator - New StandardWebSocketSession[id=0, uri=null]
15:52:12.741 [SimpleAsyncTaskExecutor-1] INFO com.blocktrust.wallet.rest.MySocketHandler - Connected
15:52:12.742 [SimpleAsyncTaskExecutor-1] INFO com.blocktrust.wallet.rest.MySocketHandler - Sending [{"type": "subscribe","channels":[{"name": "heartbeat","product_ids": ["BTC-USD"]}]}]
15:52:12.766 [SimpleAsyncTaskExecutor-1] INFO org.springframework.web.socket.client.WebSocketConnectionManager - Successfully connected
15:52:13.036 [WebSocketClient-SecureIO-2] INFO com.blocktrust.wallet.rest.MySocketHandler - Message Received [{"type":"subscriptions","channels":[{"name":"heartbeat","product_ids":["BTC-USD"]}]}]
15:52:13.300 [WebSocketClient-SecureIO-2] INFO com.blocktrust.wallet.rest.MySocketHandler - Message Received [{"type":"heartbeat","last_trade_id":41046492,"product_id":"BTC-USD","sequence":5598484730,"time":"2018-04-04T10:22:13.171000Z"}]
15:52:14.319 [WebSocketClient-SecureIO-2] INFO com.blocktrust.wallet.rest.MySocketHandler - Message Received [{"type":"heartbeat","last_trade_id":41046492,"product_id":"BTC-USD","sequence":5598484860,"time":"2018-04-04T10:22:14.172000Z"}]
15:52:15.297 [WebSocketClient-SecureIO-2] INFO com.blocktrust.wallet.rest.MySocketHandler - Message Received [{"type":"heartbeat","last_trade_id":41046492,"product_id":"BTC-USD","sequence":5598484941,"time":"2018-04-04T10:22:15.172000Z"}]
But when I run same code with the sandbox URL, It gives error:
15:54:01.526 [main] INFO org.springframework.web.socket.client.WebSocketConnectionManager - Starting WebSocketConnectionManager
15:54:01.535 [main] INFO org.springframework.web.socket.client.WebSocketConnectionManager - Connecting to WebSocket at wss://ws-feed-public.sandbox.gdax.com
15:54:01.537 [main] DEBUG org.springframework.web.socket.client.standard.StandardWebSocketClient - Connecting to wss://ws-feed-public.sandbox.gdax.com
15:54:02.218 [SimpleAsyncTaskExecutor-1] ERROR org.springframework.web.socket.client.WebSocketConnectionManager - Failed to connect
javax.websocket.DeploymentException: The HTTP request to initiate the WebSocket connection failed
at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:485)
at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:194)
at org.springframework.web.socket.client.standard.StandardWebSocketClient$1.call(StandardWebSocketClient.java:150)
at org.springframework.web.socket.client.standard.StandardWebSocketClient$1.call(StandardWebSocketClient.java:147)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.util.concurrent.ExecutionException: javax.net.ssl.SSLException: Received fatal alert: handshake_failure
at org.apache.tomcat.websocket.AsyncChannelWrapperSecure$WrapperFuture.get(AsyncChannelWrapperSecure.java:512)
at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:343)
... 5 common frames omitted
Caused by: javax.net.ssl.SSLException: Received fatal alert: handshake_failure
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1666)
at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1634)
at sun.security.ssl.SSLEngineImpl.recvAlert(SSLEngineImpl.java:1800)
at sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:1083)
at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:907)
at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:781)
at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624)
at org.apache.tomcat.websocket.AsyncChannelWrapperSecure$WebSocketSslHandshakeThread.run(AsyncChannelWrapperSecure.java:392)
Java version:
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
Debug log for SSL
[Raw read]: length = 5
0000: 15 03 01 00 02 .....
[Raw read]: length = 2
0000: 02 28 .(
Thread-2, READ: TLSv1 Alert, length = 2
Thread-2, RECV TLSv1.2 ALERT: fatal, handshake_failure
Thread-2, fatal: engine already closed. Rethrowing javax.net.ssl.SSLException: Received fatal alert: handshake_failure
Thread-2, fatal: engine already closed. Rethrowing javax.net.ssl.SSLException: Received fatal alert: handshake_failure
09:44:20.205 [SimpleAsyncTaskExecutor-1] ERROR org.springframework.web.socket.client.WebSocketConnectionManager - Failed to connect
来源:https://stackoverflow.com/questions/49648694/java-websocket-client-not-working-with-gdax-sandbox-environment