Skip to content

Instantly share code, notes, and snippets.

@riyafa
Last active October 2, 2018 02:34
Show Gist options
  • Save riyafa/fe948e7af715b07378b864d76b2cec25 to your computer and use it in GitHub Desktop.
Save riyafa/fe948e7af715b07378b864d76b2cec25 to your computer and use it in GitHub Desktop.
A working ballerina code using theWebSocket client connector
import ballerina/io;
import ballerina/log;
import ballerina/http;
int currentCaseId = 0;
int caseCounts = 0;
string ws_uri = "ws://localhost:9001";
public function main() {
openWebSocket(ws_uri + "/getCaseCount");
//openWebSocket(ws_uri + "/updateReports?agent=ballerinax");
}
function openWebSocket(string url) {
endpoint http:WebSocketClient wsClientEp {
url: url,
callbackService: basic
};
}
@http:WebSocketServiceConfig {
path: "/",
maxFrameSize: 1000000000
}
service<http:WebSocketClientService> basic {
onText(endpoint caller, string text, boolean final) {
if (currentCaseId == 0){
caseCounts = untaint check <int>text;
io:println("case counts: " + caseCounts);
} else {
caller->pushText(text, final = final) but { error e => log:printError(
"Error sending response", err = e) };
}
}
onBinary(endpoint caller, byte[] b, boolean final) {
caller->pushBinary(b, final=final) but { error e => log:printError(
"Error sending response", err = e) };
}
onClose(endpoint caller, int statusCode, string reason) {
io:println("In onClose resource");
execute();
}
onError(endpoint caller, error err) {
io:println("In onError resource");
execute();
}
}
function execute(){
currentCaseId = currentCaseId + 1;
if (currentCaseId <= caseCounts) {
openWebSocket(ws_uri + "/runCase?case=" + currentCaseId + "&agent=ballerinax");
io:println("Executing test case " + currentCaseId + "/" + caseCounts);
} else if (currentCaseId == caseCounts + 1){
openWebSocket(ws_uri + "/updateReports?agent=ballerinax");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment