From 6a7cd1dab48617b7e4170d028a00413516444a01 Mon Sep 17 00:00:00 2001 From: Stefan Rehlig Date: Fri, 15 Oct 2021 19:38:22 +0200 Subject: [PATCH] Enable ws connection timeout Add a new configuration parameter CONNECT_TIMEOUT_IN_MS to specify the websocket connection timeout in milliseconds and use it when creating `WebSocketClient`. The default value is `0`, which means _no timeout_. --- .../src/main/java/eu/chargetime/ocpp/JSONConfiguration.java | 1 + .../main/java/eu/chargetime/ocpp/WebSocketTransmitter.java | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/OCPP-J/src/main/java/eu/chargetime/ocpp/JSONConfiguration.java b/OCPP-J/src/main/java/eu/chargetime/ocpp/JSONConfiguration.java index b6a67943e..0c610d003 100644 --- a/OCPP-J/src/main/java/eu/chargetime/ocpp/JSONConfiguration.java +++ b/OCPP-J/src/main/java/eu/chargetime/ocpp/JSONConfiguration.java @@ -36,6 +36,7 @@ public class JSONConfiguration { public static final String PING_INTERVAL_PARAMETER = "PING_INTERVAL"; public static final String USERNAME_PARAMETER = "USERNAME"; public static final String PASSWORD_PARAMETER = "PASSWORD"; + public static final String CONNECT_TIMEOUT_IN_MS_PARAMETER = "CONNECT_TIMEOUT_IN_MS"; private final HashMap parameters = new HashMap<>(); diff --git a/OCPP-J/src/main/java/eu/chargetime/ocpp/WebSocketTransmitter.java b/OCPP-J/src/main/java/eu/chargetime/ocpp/WebSocketTransmitter.java index a3851a56a..4689ef115 100644 --- a/OCPP-J/src/main/java/eu/chargetime/ocpp/WebSocketTransmitter.java +++ b/OCPP-J/src/main/java/eu/chargetime/ocpp/WebSocketTransmitter.java @@ -84,8 +84,11 @@ public void connect(String uri, RadioEvents events) { httpHeaders.put("Authorization", "Basic " + new String(base64Credentials)); } + int connectTimeout = + this.configuration.getParameter(JSONConfiguration.CONNECT_TIMEOUT_IN_MS_PARAMETER, 0); + client = - new WebSocketClient(resource, draft, httpHeaders) { + new WebSocketClient(resource, draft, httpHeaders, connectTimeout) { @Override public void onOpen(ServerHandshake serverHandshake) { logger.debug("On connection open (HTTP status: {})", serverHandshake.getHttpStatus());