private void connect() throws Exception { wsSession = null; client = new WebSocketClient(); client.start(); client.setAsyncWriteTimeout(5000); client.setConnectTimeout(2000); client.setStopTimeout(5000); ClientUpgradeRequest request = new ClientUpgradeRequest(); request.setHeader("Authorization", SSHUtil.getPublicKeyAuthorization(username)); Future<Session> future = client.connect(this, uri, request); logger.info(String.format("Connecting to : %s", uri)); try { wsSession = future.get(); } catch (Exception e) { logger.log(Level.INFO, "Exception while trying to connect", e); try { client.stop(); client.destroy(); } catch (Exception e1) { logger.log(Level.INFO, "Exception while trying to disconnect", e1); } } } @OnWebSocketClose