在HandshakeCompletedEvent之后,WSS Handshake立即取消

时间:2014-10-17 21:33:58

标签: java javascript ssl websocket

我正在尝试用Java编写WSS服务器:

String certificateName = "keystore";
String path = "C:\\Users\\Black\\Desktop\\" + certificateName + ".jks";
char[] passphrase = "123456".toCharArray();

KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(path), passphrase);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(keyStore, passphrase);

SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kmf.getKeyManagers(), null, null);

SSLServerSocketFactory sslserversocketfactory = ctx.getServerSocketFactory();
serverSocket = (SSLServerSocket) sslserversocketfactory.createServerSocket(port);
serverSocket.setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1","TLSv1.2", "SSLv3" });

clientSocket = (SSLSocket) serverSocket.accept();
clientSocket.addHandshakeCompletedListener(new HandshakeCompletedListener() {
            @Override
            public void handshakeCompleted(
                    HandshakeCompletedEvent arg0) {
                System.out.println(arg0.toString());
            }
        });

out = clientSocket.getOutputStream();
outPrint = new PrintWriter(out, true);
reader = new Reader(this, clientSocket.getInputStream());
reader.start();

在控制台中,它打印出HandshakeCompletedEvent,然后立即失去连接(bufferedinputreader.read()返回-1)

我在JS的客户

var serviceUrl = 'wss://localhost:9898/';
var protocol = 'AX1';
var socket = new WebSocket(serviceUrl, protocol);

socket.onopen = function () {
    console.log('Connection Established!');
    setInterval(test(socket), 1000);
};

socket.onclose = function () {
    console.log('Connection Closed!');
};

socket.onerror = function (error) {
    console.log('Error Occured: ' + error);
};

socket.onmessage = function (e) {
    var obj = JSON.parse(e.data);
    console.log("Type: " + obj.type + " | Specific: "+obj.specific);
};

返回:

WebSocket connection to 'wss://localhost:9898/' failed: WebSocket opening handshake was canceled
Error Occured: [object Event]

密钥库是自签名的,并且生成了java keytool。我无法弄清楚错误,因为我没有得到任何例外。

客户使用正确的协议/密码套装,因为我已在socket.accept()之后将其打印出来。

0 个答案:

没有答案