Search in sources :

Example 1 with WebSocketListener

use of org.web3j.protocol.websocket.WebSocketListener in project besu by hyperledger.

the class BesuNode method checkIfWebSocketEndpointIsAvailable.

private void checkIfWebSocketEndpointIsAvailable(final String url) {
    final WebSocketClient webSocketClient = new WebSocketClient(URI.create(url));
    // Web3j implementation always invoke the listener (even when one hasn't been set). We are using
    // this stub implementation to avoid a NullPointerException.
    webSocketClient.setListener(new WebSocketListener() {

        @Override
        public void onMessage(final String message) {
        // DO NOTHING
        }

        @Override
        public void onError(final Exception e) {
        // DO NOTHING
        }

        @Override
        public void onClose() {
        // DO NOTHING
        }
    });
    // Because we can't trust the connection timeout of the WebSocket client implementation, we are
    // using this approach to verify if the endpoint is enabled.
    webSocketClient.connect();
    try {
        Awaitility.await().atMost(5, TimeUnit.SECONDS).until(webSocketClient::isOpen);
    } catch (final ConditionTimeoutException e) {
        throw new WebsocketNotConnectedException();
    } finally {
        webSocketClient.close();
    }
}
Also used : WebSocketListener(org.web3j.protocol.websocket.WebSocketListener) ConditionTimeoutException(org.awaitility.core.ConditionTimeoutException) WebsocketNotConnectedException(org.java_websocket.exceptions.WebsocketNotConnectedException) WebSocketClient(org.web3j.protocol.websocket.WebSocketClient) ConditionTimeoutException(org.awaitility.core.ConditionTimeoutException) WebsocketNotConnectedException(org.java_websocket.exceptions.WebsocketNotConnectedException) ConnectException(java.net.ConnectException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 ConditionTimeoutException (org.awaitility.core.ConditionTimeoutException)1 WebsocketNotConnectedException (org.java_websocket.exceptions.WebsocketNotConnectedException)1 WebSocketClient (org.web3j.protocol.websocket.WebSocketClient)1 WebSocketListener (org.web3j.protocol.websocket.WebSocketListener)1