-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.bal
74 lines (65 loc) · 2.54 KB
/
main.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import ballerina/io;
import ballerina/lang.runtime;
import ballerina/log;
import ballerina/websocket;
websocket:ClientAuthConfig auth = {
username: "ballerina",
issuer: "wso2",
audience: ["ballerina", "ballerina.org", "ballerina.io"],
keyId: "5a0b754-895f-4279-8843-b745e11a57e9",
jwtId: "JlbmMiOiJBMTI4Q0JDLUhTMjU2In",
customClaims: {"scp": "admin"},
expTime: 3600,
signatureConfig: {
config: {
keyFile: "../resources/private.key"
}
}
};
websocket:ClientSecureSocket secureSocket = {
cert: "../resources/public.crt"
};
type RequestMessage record {
string id;
string name;
string email;
string phone;
string dob;
};
public function main(string backend = "Backend_ABC") returns error? {
websocket:Client abcClient = check new ("wss://localhost:9090/gateway",
customHeaders = {ID: backend}, readTimeout = 30, writeTimeout = 30,
auth = auth, secureSocket = secureSocket
);
// Spawn two concurrent tasks to read and write messages to the backend.
future<error> f1 = start readMessagesFromBackend(abcClient, backend);
future<error> f2 = start sendMessageToBackend(abcClient, backend);
// Wait for both the futures to complete. They complete only if an error occurs.
map<error> errors = wait {f1, f2};
io:println("Error occured while reading or writing to backend: ", errors);
}
// Read messages from the given backend.
function readMessagesFromBackend(websocket:Client chatClient, string backendId) returns error {
while true {
anydata|websocket:Error message = chatClient->readMessage();
if message is websocket:Error {
log:printError("Error occured while writing to backend 1: ", 'error = message, backendId = backendId);
return message;
} else {
io:println(string `${backendId} >> Client: ${message.toString()}`);
}
}
}
// Send messages to the given backend in 5 second intervals.
function sendMessageToBackend(websocket:Client backendClient, string backendId) returns error {
while true {
RequestMessage payload = {id: "1", name: "John Doe", email: "[email protected]", phone: "123-456-7890", dob: "05/29/2003"};
io:println(string `Client >> ${backendId}: ${payload.toString()}`);
websocket:Error? status = backendClient->writeMessage(payload);
if status is websocket:Error {
log:printError("Error writing the message to backend", 'error = status, backendId = backendId);
return status;
}
runtime:sleep(5);
}
}