Java websocket客户端无法使用GDAX沙箱环境

时间:2018-04-04 10:27:12

标签: java spring websocket spring-websocket gdax-api

我使用spring WebSocket WebSocketClient连接GDAX服务器。 它在Live环境中运行良好,但相同的代码不适用于沙箱环境。

以下是连接服务器的代码:

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() + "]");
    }
}

以上代码与Live URL一起正常工作,输出为:

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"}]

但是当我使用沙盒URL运行相同的代码时,它会出错:

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版:

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)

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

0 个答案:

没有答案