vendredi 23 février 2018

Error in connection establishment: net::ERR_CONNECTION_CLOSED when connecting to jetty ssl Server

I'm trying to connect a javascript to a java jetty server. Due to using https I will have to use wss (ws worked with http fine) The keystore I use is created by my normal certificate that I use for https too with the java keytools. This is how I create the server:

System.out.println("Setting up jetty server...");
        server = new Server(10061);

        System.out.println("setting handler..");

        System.out.println("configuring ssl support...");
        SslContextFactory contexfactory = new SslContextFactory();
        contexfactory.setKeyStorePath("/home/keystore");
        contexfactory.setKeyStorePassword("********");
        contexfactory.setKeyManagerPassword("********");
        SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(contexfactory, org.eclipse.jetty.http.HttpVersion.HTTP_1_1.toString());

        HttpConfiguration config = new HttpConfiguration();
        config.setSecureScheme("https");
        config.setSecurePort(10062);
        config.setOutputBufferSize(32786);
        config.setRequestHeaderSize(8192);
        config.setResponseHeaderSize(8192);
        HttpConfiguration sslConfig = new HttpConfiguration(config);
        sslConfig.addCustomizer(new SecureRequestCustomizer());
        HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(sslConfig);
        connector = new ServerConnector(server, sslConnectionFactory, httpConnectionFactory);
        connector.setPort(10062);
        server.addConnector(connector);

        ServletContextHandler wscontext = new ServletContextHandler(ServletContextHandler.SESSIONS);
        wscontext.setContextPath("/");
        wscontext.setResourceBase(".");
        wscontext.setClassLoader(Thread.currentThread().getContextClassLoader());

        wscontext.addServlet(new ServletHolder("cubenex", CubenexServlet.class), "/");

        wscontext.setHandler(new WebSocketHandler() {
            @Override
            public void configure(WebSocketServletFactory factory) {
                factory.register(Handler.class);
            }
        });
        server.setHandler(wscontext);

        webServerThread = new Thread(new Runnable() {
            public void run() {
                try {
                    server.start();
                    server.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        webServerThread.start();
        System.out.println("Jetty server set up!");
    } catch (Exception e) {
        e.printStackTrace();
    }

The jetty server causes no errors and I get the confirmation 2018-02-23 08:21:38.987:INFO:oejs.ServerConnector:Thread-1: Started ServerConnector@6356e82c{SSL-HTTP/1.1}{0.0.0.0:10062}

When I connect with javascript as following:

var ws = new WebSocket("wss://***.**.***.**:10062/");
    ws.onopen = function(){
        ws.send("login "+user+" "+password);
    }
    ws.onmessage = function(msg){
        console.log(msg.data);
    } 

I get the error:

WebSocket connection to 'wss://137.74.140.73:10062/' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED




Aucun commentaire:

Enregistrer un commentaire