diff --git a/distributor/src/distributor/website.bal b/distributor/src/distributor/website.bal index 4bd6a8e..4eab78f 100644 --- a/distributor/src/distributor/website.bal +++ b/distributor/src/distributor/website.bal @@ -2,6 +2,7 @@ import ballerina/file; import ballerina/http; import ballerina/log; import ballerina/mime; +import ballerina/task; import ballerina/time; import ballerina/xmlutils; @@ -439,3 +440,31 @@ function generateParliamentaryResultsTable() returns string { tab = tab + ""; return tab; } + +task:TimerConfiguration timerConfiguration = { + intervalInMillis: 30000, + initialDelayInMillis: 30000 +}; +listener task:Listener timer = new (timerConfiguration); + +service timerService on timer { + resource function onTrigger() { + string[] keys = jsonConnections.keys(); + foreach string k in keys { + http:WebSocketCaller? con = jsonConnections[k]; + if !(con is ()) { + log:printDebug("Pinging " + con.getConnectionId()); + _ = start ping(con); + } + } + } +} + +final byte[] pingData = "ping".toBytes(); + +function ping(http:WebSocketCaller con) { + var err = con->ping(pingData); + if (err is http:WebSocketError) { + log:printError(string `Error pinging ${con.getConnectionId()}`, err); + } +} diff --git a/distributor/web/active-2020-07-28-I b/distributor/web/active-2020-07-28-II similarity index 100% rename from distributor/web/active-2020-07-28-I rename to distributor/web/active-2020-07-28-II diff --git a/distributor/web/info.txt b/distributor/web/info.txt index b36d46c..d9e108d 100644 --- a/distributor/web/info.txt +++ b/distributor/web/info.txt @@ -2,15 +2,15 @@ ****** IMPORTANT ******* ** -** The latest version of the subscriber JAR is subscriber-20200728-I.jar +** The latest version of the subscriber JAR is subscriber-20200728-II.jar ** -** Available at https://github.com/ECLK/Results-Dist/releases/tag/v2020-07-28-I +** Available at https://github.com/ECLK/Results-Dist/releases/tag/v2020-07-28-II ** ****** IMPORTANT ******* Run it as follows: -java -jar subscriber-20200728-I.jar [options] +java -jar subscriber-20200728-II.jar [options] where options are: -username=name my username for authentication diff --git a/subscriber/src/subscriber/constants.bal b/subscriber/src/subscriber/constants.bal index 859f514..c18308e 100644 --- a/subscriber/src/subscriber/constants.bal +++ b/subscriber/src/subscriber/constants.bal @@ -9,7 +9,7 @@ const LEVEL_NF = "NATIONAL-FINAL"; const WANT_IMAGE = "image=true"; const WANT_AWAIT_RESULTS = "await=true"; -const MY_VERSION = "2020-07-28-I"; +const MY_VERSION = "2020-07-28-II"; const UNDERSOCRE = "_"; const COLON = ":"; diff --git a/subscriber/src/subscriber/subscriber.bal b/subscriber/src/subscriber/subscriber.bal index 33debb4..d06e394 100644 --- a/subscriber/src/subscriber/subscriber.bal +++ b/subscriber/src/subscriber/subscriber.bal @@ -4,6 +4,7 @@ import ballerina/io; import ballerina/log; import maryamzi/sound; +import ballerina/lang.'string; boolean wantJson = false; boolean wantXml = false; @@ -164,6 +165,10 @@ service resultDataOnlyClientService = @http:WebSocketServiceConfig {} service { resource function onClose(http:WebSocketClient wsEp, int statusCode, string reason) { log:printInfo(string `Connection closed: statusCode: ${statusCode}, reason: ${reason}`); } + + resource function onPing(http:WebSocketClient wsEp, byte[] data) { + logPingIfRequired(wsEp, data); + } }; service awaitAndResultDataClientService = @http:WebSocketServiceConfig {} service { @@ -191,6 +196,10 @@ service awaitAndResultDataClientService = @http:WebSocketServiceConfig {} servic resource function onClose(http:WebSocketClient wsEp, int statusCode, string reason) { log:printInfo(string `Connection closed: statusCode: ${statusCode}, reason: ${reason}`); } + + resource function onPing(http:WebSocketClient wsEp, byte[] data) { + logPingIfRequired(wsEp, data); + } }; service imageAndResultDataClientService = @http:WebSocketServiceConfig {} service { @@ -220,6 +229,10 @@ service imageAndResultDataClientService = @http:WebSocketServiceConfig {} servic resource function onClose(http:WebSocketClient wsEp, int statusCode, string reason) { log:printInfo(string `Connection closed: statusCode: ${statusCode}, reason: ${reason}`); } + + resource function onPing(http:WebSocketClient wsEp, byte[] data) { + logPingIfRequired(wsEp, data); + } }; @@ -257,6 +270,10 @@ service allClientService = @http:WebSocketServiceConfig {} service { resource function onClose(http:WebSocketClient wsEp, int statusCode, string reason) { log:printInfo(string `Connection closed: statusCode: ${statusCode}, reason: ${reason}`); } + + resource function onPing(http:WebSocketClient wsEp, byte[] data) { + logPingIfRequired(wsEp, data); + } }; function notifyAwait() { @@ -265,3 +282,10 @@ function notifyAwait() { log:printError("Error pinging on await notification", pingStatus); } } + +function logPingIfRequired(http:WebSocketClient wsEp, byte[] data) { + log:printDebug(function () returns string { + string|error res = 'string:fromBytes(data); + return "onPing: " + (res is string ? res : data.toString()); + }); +}