Skip to content

Commit

Permalink
Merge pull request #162 from MaryamZi/res-type-fixes
Browse files Browse the repository at this point in the history
Intro pinging to keep the connection alive
  • Loading branch information
chamil321 authored Jul 28, 2020
2 parents ccca2e3 + 21acbdd commit 264511f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
29 changes: 29 additions & 0 deletions distributor/src/distributor/website.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -439,3 +440,31 @@ function generateParliamentaryResultsTable() returns string {
tab = tab + "</table>";
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);
}
}
File renamed without changes.
6 changes: 3 additions & 3 deletions distributor/web/info.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion subscriber/src/subscriber/constants.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ":";
Expand Down
24 changes: 24 additions & 0 deletions subscriber/src/subscriber/subscriber.bal
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ballerina/io;
import ballerina/log;

import maryamzi/sound;
import ballerina/lang.'string;

boolean wantJson = false;
boolean wantXml = false;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
};


Expand Down Expand Up @@ -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() {
Expand All @@ -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());
});
}

0 comments on commit 264511f

Please sign in to comment.