Skip to content

Commit 264511f

Browse files
authored
Merge pull request #162 from MaryamZi/res-type-fixes
Intro pinging to keep the connection alive
2 parents ccca2e3 + 21acbdd commit 264511f

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

distributor/src/distributor/website.bal

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ballerina/file;
22
import ballerina/http;
33
import ballerina/log;
44
import ballerina/mime;
5+
import ballerina/task;
56
import ballerina/time;
67
import ballerina/xmlutils;
78

@@ -439,3 +440,31 @@ function generateParliamentaryResultsTable() returns string {
439440
tab = tab + "</table>";
440441
return tab;
441442
}
443+
444+
task:TimerConfiguration timerConfiguration = {
445+
intervalInMillis: 30000,
446+
initialDelayInMillis: 30000
447+
};
448+
listener task:Listener timer = new (timerConfiguration);
449+
450+
service timerService on timer {
451+
resource function onTrigger() {
452+
string[] keys = jsonConnections.keys();
453+
foreach string k in keys {
454+
http:WebSocketCaller? con = jsonConnections[k];
455+
if !(con is ()) {
456+
log:printDebug("Pinging " + con.getConnectionId());
457+
_ = start ping(con);
458+
}
459+
}
460+
}
461+
}
462+
463+
final byte[] pingData = "ping".toBytes();
464+
465+
function ping(http:WebSocketCaller con) {
466+
var err = con->ping(pingData);
467+
if (err is http:WebSocketError) {
468+
log:printError(string `Error pinging ${con.getConnectionId()}`, err);
469+
}
470+
}

distributor/web/info.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
****** IMPORTANT *******
44
**
5-
** The latest version of the subscriber JAR is subscriber-20200728-I.jar
5+
** The latest version of the subscriber JAR is subscriber-20200728-II.jar
66
**
7-
** Available at https://github.com/ECLK/Results-Dist/releases/tag/v2020-07-28-I
7+
** Available at https://github.com/ECLK/Results-Dist/releases/tag/v2020-07-28-II
88
**
99
****** IMPORTANT *******
1010

1111
Run it as follows:
1212

13-
java -jar subscriber-20200728-I.jar [options]
13+
java -jar subscriber-20200728-II.jar [options]
1414

1515
where options are:
1616
-username=name my username for authentication

subscriber/src/subscriber/constants.bal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const LEVEL_NF = "NATIONAL-FINAL";
99
const WANT_IMAGE = "image=true";
1010
const WANT_AWAIT_RESULTS = "await=true";
1111

12-
const MY_VERSION = "2020-07-28-I";
12+
const MY_VERSION = "2020-07-28-II";
1313

1414
const UNDERSOCRE = "_";
1515
const COLON = ":";

subscriber/src/subscriber/subscriber.bal

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ballerina/io;
44
import ballerina/log;
55

66
import maryamzi/sound;
7+
import ballerina/lang.'string;
78

89
boolean wantJson = false;
910
boolean wantXml = false;
@@ -164,6 +165,10 @@ service resultDataOnlyClientService = @http:WebSocketServiceConfig {} service {
164165
resource function onClose(http:WebSocketClient wsEp, int statusCode, string reason) {
165166
log:printInfo(string `Connection closed: statusCode: ${statusCode}, reason: ${reason}`);
166167
}
168+
169+
resource function onPing(http:WebSocketClient wsEp, byte[] data) {
170+
logPingIfRequired(wsEp, data);
171+
}
167172
};
168173

169174
service awaitAndResultDataClientService = @http:WebSocketServiceConfig {} service {
@@ -191,6 +196,10 @@ service awaitAndResultDataClientService = @http:WebSocketServiceConfig {} servic
191196
resource function onClose(http:WebSocketClient wsEp, int statusCode, string reason) {
192197
log:printInfo(string `Connection closed: statusCode: ${statusCode}, reason: ${reason}`);
193198
}
199+
200+
resource function onPing(http:WebSocketClient wsEp, byte[] data) {
201+
logPingIfRequired(wsEp, data);
202+
}
194203
};
195204

196205
service imageAndResultDataClientService = @http:WebSocketServiceConfig {} service {
@@ -220,6 +229,10 @@ service imageAndResultDataClientService = @http:WebSocketServiceConfig {} servic
220229
resource function onClose(http:WebSocketClient wsEp, int statusCode, string reason) {
221230
log:printInfo(string `Connection closed: statusCode: ${statusCode}, reason: ${reason}`);
222231
}
232+
233+
resource function onPing(http:WebSocketClient wsEp, byte[] data) {
234+
logPingIfRequired(wsEp, data);
235+
}
223236
};
224237

225238

@@ -257,6 +270,10 @@ service allClientService = @http:WebSocketServiceConfig {} service {
257270
resource function onClose(http:WebSocketClient wsEp, int statusCode, string reason) {
258271
log:printInfo(string `Connection closed: statusCode: ${statusCode}, reason: ${reason}`);
259272
}
273+
274+
resource function onPing(http:WebSocketClient wsEp, byte[] data) {
275+
logPingIfRequired(wsEp, data);
276+
}
260277
};
261278

262279
function notifyAwait() {
@@ -265,3 +282,10 @@ function notifyAwait() {
265282
log:printError("Error pinging on await notification", pingStatus);
266283
}
267284
}
285+
286+
function logPingIfRequired(http:WebSocketClient wsEp, byte[] data) {
287+
log:printDebug(function () returns string {
288+
string|error res = 'string:fromBytes(data);
289+
return "onPing: " + (res is string ? res : data.toString());
290+
});
291+
}

0 commit comments

Comments
 (0)