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();
}
}
Aggregations