如何解决Java Websocket客户端JSR 365连接问题
我正在尝试使用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,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();
}
}
所以任何人都可以在这里帮助我,这可能是导致无法连接的原因。
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。