Jetty 9 WebSocket client issue when using secure connection


Alex

I am writing a WebSocket client in a Java application using the Jetty 9.4.18 library.

I'm new to WebSockets, so I started to test using the two example classes from the Jetty documentation, and then connect toecho.websocket.org

When connecting without SSL, the test runs fine, but when connecting towss://echo.websocket.org

I always get the same exception:

java.io.EOFException: HttpConnectionOverHTTP@50371e9d::DecryptedEndPoint@6dc65fc2{echo.websocket.org/174.129.224.73:443<->/192.168.1.34:60521,OPEN,fill=-,flush=C,to=226/0}
    at org.eclipse.jetty.client.http.HttpReceiverOverHTTP.earlyEOF(HttpReceiverOverHTTP.java:338)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:1551)
    at org.eclipse.jetty.client.http.HttpReceiverOverHTTP.shutdown(HttpReceiverOverHTTP.java:209)
    at org.eclipse.jetty.client.http.HttpReceiverOverHTTP.process(HttpReceiverOverHTTP.java:147)
    at org.eclipse.jetty.client.http.HttpReceiverOverHTTP.receive(HttpReceiverOverHTTP.java:73)
    at org.eclipse.jetty.client.http.HttpChannelOverHTTP.receive(HttpChannelOverHTTP.java:133)
    at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.onFillable(HttpConnectionOverHTTP.java:155)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:305)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
    at org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.onFillable(SslConnection.java:411)
    at org.eclipse.jetty.io.ssl.SslConnection.onFillable(SslConnection.java:305)
    at org.eclipse.jetty.io.ssl.SslConnection$2.succeeded(SslConnection.java:159)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
    at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:765)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:683)
    at java.lang.Thread.run(Thread.java:748)

It looks like the server is not responding to the handshake request on shutdown.

I know about SslContextFactory, but my understanding is that it should only be used if you need your own TrustStore or KeyStore or other special cases.

Also note that after trying unsuccessfully, I downloaded another websocket implementation from https://github.com/TooTallNate/Java-WebSocket and it works fine on both ws and wss without having to set up something special for SSL. But for this project I have to use Jetty.

The code I'm using is exactly the example from the Jetty documentation at https://www.eclipse.org/jetty/documentation/9.4.x/jetty-websocket-client-api.html

The only change I made was adding an onError method to SimpleEchoSocket which will dump the full exception stack.

Am I missing something?

Thanks in advance!

Joakim Erdfelt:

Unfortunately, websocket.org(and the Kaazing host/proxy) have a lot of TLS issues at the moment, so using a public server is not a wise choice right now.

Here's another demo, also using TLS and WebSocket, against a stackexchange server with a proper and reasonable TLS/SSL implementation.

This was written against Jetty 9.4.18.v20190429

package org.eclipse.jetty.demo;

import java.net.URI;
import java.util.concurrent.Future;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.client.WebSocketClient;

@WebSocket
public class SecureClientSocket
{
    private static final Logger LOG = Log.getLogger(SecureClientSocket.class);

    public static void main(String[] args)
    {
        String url = "wss://qa.sockets.stackexchange.com/";

        SslContextFactory ssl = new SslContextFactory.Client();
        ssl.setEndpointIdentificationAlgorithm("HTTPS");
        HttpClient http = new HttpClient(ssl);
        WebSocketClient client = new WebSocketClient(http);
        try
        {
            http.start();
            client.start();
            SecureClientSocket socket = new SecureClientSocket();
            Future<Session> fut = client.connect(socket, URI.create(url));
            Session session = fut.get();
            session.getRemote().sendString("Hello");
            session.getRemote().sendString("155-questions-active");
        }
        catch (Throwable t)
        {
            LOG.warn(t);
        }
        finally
        {
            stop(http);
            stop(client);
        }
    }

    private static void stop(LifeCycle lifeCycle)
    {
        try
        {
            lifeCycle.stop();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @OnWebSocketConnect
    public void onConnect(Session sess)
    {
        LOG.info("onConnect({})", sess);
    }

    @OnWebSocketClose
    public void onClose(int statusCode, String reason)
    {
        LOG.info("onClose({}, {})", statusCode, reason);
    }

    @OnWebSocketError
    public void onError(Throwable cause)
    {
        LOG.warn(cause);
    }

    @OnWebSocketMessage
    public void onMessage(String msg)
    {
        LOG.info("onMessage() - {}", msg);
    }
}

Related


Jetty 9 WebSocket client issue when using secure connection

Alex I am writing a WebSocket client in a Java application using the Jetty 9.4.18 library. I'm new to WebSockets, so I started to test using the two example classes from the Jetty documentation, and then connect toecho.websocket.org When connecting without SSL

Jetty 9 WebSocket client issue when using secure connection

Alex I am writing a WebSocket client in a Java application using the Jetty 9.4.18 library. I'm new to WebSockets, so I started to test using the two example classes from the Jetty documentation, and then connect toecho.websocket.org When connecting without SSL

Jetty 9 WebSocket client issue when using secure connection

Alex I am writing a WebSocket client in a Java application using the Jetty 9.4.18 library. I'm new to WebSockets, so I started to test using the two example classes from the Jetty documentation, and then connect toecho.websocket.org When connecting without SSL

Jetty 9 WebSocket client issue when using secure connection

Alex I am writing a WebSocket client in a Java application using the Jetty 9.4.18 library. I'm new to WebSockets, so I started to test using the two example classes from the Jetty documentation, and then connect toecho.websocket.org When connecting without SSL

Secure connection using transport client

St. Kaidal Need to connect to secure elasticsearch with https authentication using transport client in java code. I have the user id and password to connect, but need an example of how we do it? I am using elastic search 5.6.0. I'm looking for xpack and shelf

Secure connection using transport client

St. Kaidal Need to connect to secure elasticsearch with https authentication using transport client in java code. I have the user id and password to connect, but need an example of how we do it? I am using elastic search 5.6.0. I'm looking for xpack and shelf

Web socket issue Secure connection with Jetty web server

DN0300 I'm having trouble getting my Websocket secure (wss://) connection to work. Recently I had to use the server on the previous server wss:// connection, and switched servers and got a new SSL certificate for it to work. On the new server, I generated a ne

Web socket issue Secure connection with Jetty web server

DN0300 I'm having trouble getting my Websocket secure (wss://) connection to work. Recently I had to use the server on the previous server wss:// connection, and switched servers and got a new SSL certificate for it to work. On the new server, I generated a ne

Simple Websocket Client in Java - Connection Issue

Flint Heart: I'm trying to write a simple websocket client in Java to connect to a 3rd party WebSocket server (which I don't have access to). I can connect and communicate with the websocket server using javascript in the browser, but when I try to do the same

Simple Websocket Client in Java - Connection Issue

Finsart I'm trying to write a simple websocket client in Java to connect to a 3rd party WebSocket server (which I don't have access to). I can connect and communicate with the websocket server using javascript in the browser, but when I try to do the same with

Simple Websocket Client in Java - Connection Issue

Flint Heart: I'm trying to write a simple websocket client in Java to connect to a 3rd party WebSocket server (which I don't have access to). I can connect and communicate with the websocket server using javascript in the browser, but when I try to do the same

Extract client X509 certificate from secure Websocket connection

Guillaume Pansier I want to create certificate based authentication on top of websocket communication. So I created a websocket serverEndpoint and set up SSL for client authentication with the help of jetty like this: Server server = new Server(); //Create SS

Extract client X509 certificate from secure Websocket connection

Guillaume Pansier I want to create certificate based authentication on top of websocket communication. So I created a websocket serverEndpoint and set up SSL for client authentication with the help of jetty like this: Server server = new Server(); //Create SS

jetty websocket class loading issue

svk I have implemented a basic websocket server in Jetty (standalone mode). MyWebSocketServlet.java public class MyWebSocketServlet extends WebSocketServlet { @Override public void configure(WebSocketServletFactory webSocketServletFactory){

jetty websocket class loading issue

svk I have implemented a basic websocket server in Jetty (standalone mode). MyWebSocketServlet.java public class MyWebSocketServlet extends WebSocketServlet { @Override public void configure(WebSocketServletFactory webSocketServletFactory){

How to authenticate Websocket client in jetty?

Pradeep Agarwal I am using embedded Jetty on the server side which will accept http and websocket requests. I am using org.eclipse.jetty.security.authentication.FormAuthenticator to authenticate users. Once the user is logged in, my javascript code will open a

Jetty 8.1.1 Websocket client handshake

Tomer Epstein I am using Jetty 8.1.1 Websocket client api. I need to use ("Sec-WebSocket-Protocol", "xsCrossfire")and update the title("Authorization", "Basic TLVWQMZqRr2hasYnZoI=") WebSocketClientFactory factory = new WebSocketClientFactory(); factory.start()

How to authenticate Websocket client in jetty?

Pradeep Agarwal I am using embedded Jetty on the server side which will accept http and websocket requests. I am using org.eclipse.jetty.security.authentication.FormAuthenticator to authenticate users. Once the user is logged in, my javascript code will open a

WebSocket connection issue

AtherSajjad: I have a websocket server running in a Java application written using https://github.com/TooTallNate/Java-WebSocket and trying to connect to it from a browser. The browser sends a connection request every second until the websocket server is initi

Flutter websocket connection issue

Admission to Murtusa I'm trying to develop a flutter app that can connect to a server and exchange data using websockets. The server is in .Net Core and uses Asp.Net Core Websockets for this functionality. The problem I am facing is that my flutter app is not