How to connect to SSL Server from Javascript, using self-signed certificates?
<p>I created a simple example of what I am trying to do.</p>

<p>I have the server code like this:</p>

<pre><code>package com.company;

import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class EchoServer {
    public
    static
    void
    main(String[] arstring) {
        try {

            SSLServerSocketFactory sslserversocketfactory =
                    (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
            SSLServerSocket sslserversocket =
                    (SSLServerSocket) sslserversocketfactory.createServerSocket(9999);
            SSLSocket sslsocket = (SSLSocket) sslserversocket.accept();

            InputStream inputstream = sslsocket.getInputStream();
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

            String string = null;
            while ((string = bufferedreader.readLine()) != null) {
                System.out.println(string);
                System.out.flush();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}
</code></pre>

<p>And the client code like this:</p>

<pre><code>&lt;html&gt;
&lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"&gt;
    &lt;!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --&gt;

    &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
    &lt;meta charset="utf-8"&gt;

    &lt;script type="text/javascript"&gt;
        function onClick(){
            var wrc_socket = new WebSocket('wss://localhost:9999');
            wrc_socket.onmessage = function(event) {
                alert('message = '+event.data);
            };
            wrc_socket.onclose = function(event) {
                alert('Socket is close!');
            };
            wrc_socket.onopen = function(event) {
                alert('Socket is open!');
            };
            wrc_socket.send('Hi there!');
            alert('Done!');
        }
    &lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;a href="#" onclick="onClick()"&gt;Test!&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>I have created the private and public key pair and imported it in the keystore file as it is described <a href="https://stackoverflow.com/questions/906402/importing-an-existing-x509-certificate-and-private-key-in-java-keystore-to-use-i">here</a>.</p>

<p>I ran the server application with the parameters:</p>

<pre><code>-Djavax.net.ssl.keyStore=mySrvKeystore -Djavax.net.ssl.keyStorePassword=123456 -Djavax.net.debug=all
</code></pre>

<p>When I click the Test button, I get this errors in ssl debug:</p>

<pre><code>main, WRITE: TLSv1.2 Change Cipher Spec, length = 1
[Raw write]: length = 6
0000: 14 03 03 00 01 01                                  ......
Disconnected from the target VM, address: '127.0.0.1:64118', transport: 'socket'
</code></pre>

<p>For some reason the connection is closing during the handshake. I can't figure out the reason for that, please advice the solution or some ideas to check.</p>