我正在尝试使用TooTallNate-Jave-Websocket libray连接到WSS套接字。
每当我开始连接时,都会出现此错误:
SocketClient : onClose
Invalid status code received: 421 Status line: HTTP/1.1 421 unknown protocol
1002
false
我正在以这种方式形成网址
Uri.Builder builder = new Uri.Builder();
builder.scheme("wss")
.authority(server)
.appendQueryParameter("session-id", sessionId)
.appendQueryParameter("udid", udid)
.appendQueryParameter("token", token);
logger.info(" The server URL "+ builder.build().toString() + " "+ builder.build().getScheme() + " "+ builder.build().getAuthority());
SocketClient socks = new SocketClient(new URI(builder.build().toString()));
socks.connect();
我的websocket实现非常基础,就像这样:
public static class SocketClient extends WebSocketClient{
public SocketClient(URI serverUri, Draft draft){
super(serverUri, draft);
}
public SocketClient(URI serverUri){
super(serverUri);
}
@Override
public void onMessage(String message) {
logger.info("SocketClient : OnMessage");
logger.info(message);
}
@Override
public void onOpen(ServerHandshake handshakedata) {
logger.info("SocketClient : onOpen");
logger.info(handshakedata.getHttpStatus() + "");
}
@Override
public void onClose(int code, String reason, boolean remote) {
logger.info("SocketClient : onClose");
logger.info(reason);
logger.info(code + "");
logger.info( remote + "");
}
@Override
public void onError(Exception ex) {
logger.info("SocketClient : onError");
logger.info(ex.toString());
}
}
我检查了我要连接的服务器是否具有有效的证书,并且它确实有效。所以我猜我构造URL的方式出了问题。