Java Websocket客户端(JSR 365)连接问题

时间:2020-11-10 16:28:21

标签: java java-websocket

我正在尝试使用JSR 365 API在Java中使用一种Websocket服务。此服务部署在其他第三方服务器上。因此,在调用此方法时,需要将身份验证令牌作为cookie传递到标头中。 一旦我致电此服务,就不会收到任何异常或错误。同样,org.java_websocket.client.WebSocketClient.connectBlocking()始终返回false值。 由于此代码未给出任何错误或异常,因此很难理解可能是什么原因以及如何进行故障排除以获取确切原因。 只是为了补充,我们正在从云中获取这些数据。

package com.test.mams;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.SSLContext;

import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;

public class WebSocketClientTest extends WebSocketClient {
    public WebSocketClientTest(String url, Draft draft) throws URISyntaxException {
        super(new URI(url), draft);
    }

    public void getFirmAccountData(WebSocketClientTest client, String accessToken) throws ParseException,
            URISyntaxException, JsonMappingException, UnsupportedEncodingException, JsonProcessingException {
        Map<String, String> headers = null;
        if (accessToken != null && !accessToken.isEmpty()) {
            headers = new HashMap<String, String>();
            headers.put("Cookie", "Authorization=Bearer%20" + accessToken);
        }
        try {
            // load default java key store : jssecacerts
            SSLContext sslContext = null;
            sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, null, null);
            client.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sslContext));
            boolean connected = client.connectBlocking();
            System.out.println("Value of connected : " + connected);
        } catch (Exception e) {
            e.printStackTrace();
            client.close();
        }
    }

    public static void main(String[] args)
            throws URISyntaxException, UnsupportedEncodingException, JsonMappingException, JsonProcessingException {
        Draft draft = new Draft_17();
        WebSocketClientTest client = new WebSocketClientTest(
                "wss://referencedatacloudstreams.sit.test.net/streams/delivery/account", draft);
        try {
            client.getFirmAccountData(client, "OTgyMGI5ZDUtZGM5NC00NmY5LTg4OTMtMWFmY2I4MTEzMDU4");
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onOpen(ServerHandshake handshakedata) {
        System.out.println("Connected");
    }

    @Override
    public void onMessage(String message) {
        try {
            boolean done = false;
            boolean completed = false;
            boolean error = false;
            boolean info = false;

            if (message.contains("\"status\": \"done\"")) {
                done = true;
            }
            if (message.contains("\"status\": \"complete\"")) {
                completed = true;
                done = true;
            }
            if (message.contains("\"status\": \"error\"") || message.contains("\"status\": \"warning\"")) {
                completed = false;
                done = false;
                error = true;
            }
            if (message.contains("\"status\": \"info\"")) {
                info = true;
            }

            if (done) {
                System.out.println("--DONE--   " + message);
                if (completed) {
                    System.out.println("--COMPLETED--   " + message);
                } else if (!error) {
                    // request next chunk until complete
                    this.send("{\"request\": " + 2000 + "}");
                }
            } else if (error) {
                System.out.println("Error/Warning message received :" + message);
                System.out.println("Closing web socket");
                this.close();
            } else if (info) {
                System.out.println("Received info message for subscription : " + message);
            }
        } catch (Throwable e) { // buffer.append sometimes throws out of memory exception , we have to catch it
                                // otherwise web socket will remain open.
            e.printStackTrace();
            this.close();
        }
    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        try {
            System.out.println("Websocket Close params: " + "code:" + code + ", " + "reason:" + reason + ", "
                    + "remote:" + remote);
            if (code != 1000) {
                System.out.println("Websocket did not close normally.");
            } else {
                System.out.println("Web socket closed normally for subscription");
            }
        } catch (Exception e) {
            // System.out.println("Error occured in onClose call Back for subscription
            // "+subscriptionId,e);
            e.printStackTrace();
        }
    }

    @Override
    public void onError(Exception ex) {
        ex.printStackTrace();
    }
}

所以任何人都可以在这里帮助我,这可能是导致无法连接的原因。

谢谢!

0 个答案:

没有答案