title: How to detect connection lost with Jetty WebSocket
body: <p>I use a WebSocket to ensure communication between two standalone jars. The first one launches an embedded Jetty and deploys a WebSocket, and the second is the Jetty WebSocket client that sends commands and waits for the processing of the server part.</p>
  
  <p>Server:</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.io.IOException;
  
  import javax.websocket.*;
  
  @ClientEndpoint
  @ServerEndpoint(value = "/websocket-api", encoders = {     NotificationEncoder.class }, decoders = { NotificationDecoder.class })
  public class APIWebsocket {
  
    private static final Logger log = LoggerFactory.getLogger(APIWebsocket.class);
  
    @OnOpen
    public void open(Session session) {
      log.info("Session opened...");
  
    }
  
    @OnClose
    public void close(Session session) throws IOException {
      session.close();
      log.info("Session closed !");
    }
  
    @OnError
    public void onError(Throwable error) {
      log.error("Error during websocket session : {}", error.getMessage());
      log.debug("{}", error);
    }
  
    @OnMessage
    public void handleMessage(String message, Session session) {
  
         log.info("Message received : {}", message);
  
         List&lt;Data&gt; datas = JSONHelper.toCollection(message);
         for (Data data : datas){
            // do process with average time 30sec...
            // send notification of processing
            Notification unitNotification= new Notification("PROCESSED");
            session.getBasicRemote().sendObject(unitNotification);
         }
  
        // Build and send end notification message
        Notification endProcessNotification = new Notification("END OF PROCESS");
        session.getBasicRemote().sendObject(endProcessNotification);
  
      } catch (EncodeException | IOException e) {
        log.error(e.getMessage(), e);
        // Manage error
  
      } finally {
        try {
          session.close();
        } catch (IOException e) {
          log.error(e.getMessage(), e);
          // Manage error
        }
  
      }
  
    }
  
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.io.IOException;
  import java.net.URI;
  
  import org.eclipse.jetty.websocket.api.Session;
  import org.eclipse.jetty.websocket.api.WebSocketAdapter;
  import org.eclipse.jetty.websocket.client.WebSocketClient;
  
  
  public class WebsocketClient extends WebSocketAdapter {
  
    private static final Logger log = LoggerFactory.getLogger(WebsocketClient.class);
    private Session session = null;
  
    public APIWebsocketClient(String uri) {
      try {
        WebSocketClient client = new WebSocketClient();
        client.setMaxIdleTimeout(35 * 1000L);
  
        client.start();
        client.connect(this, new URI(uri)).get();
  
      } catch (Exception e) {
        log.error(e.getMessage(), e);
      }
  
    }
  
    public void startProcess(String datas) {
      try {
        session.getRemote().sendString(datas);
      } catch (IOException e) {
        log.error(e.getMessage(), e);
      }
    }
  
    @Override
    public void onWebSocketText(String message) {
      log.info("Message received : {}", message);
    }
  
    @Override
    public void onWebSocketClose(int statusCode, String reason) {
      log.info("Websocket closed ! [statusCode={}, reason={}]", statusCode, reason);
      this.session.close();
    }
  
    @Override
    public void onWebSocketConnect(Session session) {
      this.session = session;
      log.info("Websocket Client details : {}-{}", session.getLocalAddress().getHostName(), session
          .getLocalAddress().getAddress().getHostAddress());
    }
  
    @Override
    public void onWebSocketError(Throwable cause) {
      log.error("Error during websocket execution : {}", cause.getMessage(), cause);
  
      if (this.session != null) {
        this.session.close();
      }
    }
  
  }
  </code></pre>
  
  <p>My goal is to detect if the network communication is lost between the client and the server. If I simulate a loss of network, by disconnecting the network wire, the WebSocket should detect a communication error on both server and client side.</p>
  
  <p>Do you have any idea how I could proceed to detect network loss between the client and the server?</p>