-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.bal
183 lines (131 loc) · 6.22 KB
/
server.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import ballerina/io;
import ballerina/regex;
import ballerina/websocket;
listener websocket:Listener PubSubListner = new websocket:Listener(9090);
isolated websocket:Caller[] subscriberList = [];
isolated websocket:Caller[] publisherList = [];
service /SUBSCRIBER on PubSubListner {
resource isolated function get [string topic]() returns any {
return new PubSubService("SUBSCRIBER", topic);
}
}
service /PUBLISHER on PubSubListner {
resource isolated function get [string topic]() returns any {
return new PubSubService("PUBLISHER", topic);
}
}
service class PubSubService {
*websocket:Service;
private string userType;
private string topic = "";
private string[] multipleTopics = [];
isolated function init(string userType, string topic) {
self.userType = userType;
self.multipleTopics = regex:split(topic, ",");
if (self.multipleTopics.length() == 1) {
self.topic = self.multipleTopics[0];
}
}
public isolated function onOpen(websocket:Caller caller) returns error? {
// This `remote function` is triggered when a new connection is established.
// The `caller` object represents the connected client.
io:println("New client connected");
if (self.userType === "SUBSCRIBER") {
lock {
subscriberList.push(caller);
io:println("Connected Subscribers Count: ",subscriberList.length());
}
if (self.multipleTopics.length() > 1) {
caller.setAttribute("isMultipleTopics", true);
caller.setAttribute("multipleTopics", self.multipleTopics);
}
caller.setAttribute("topic", self.topic);
}
else if (self.userType === "PUBLISHER") {
lock {
publisherList.push(caller);
io:println("Connected Publishers Count: ",publisherList.length());
}
if (self.multipleTopics.length() > 1) {
caller.setAttribute("isMultipleTopics", true);
caller.setAttribute("multipleTopics", self.multipleTopics);
}
caller.setAttribute("topic", self.topic);
}
}
// This `remote function` is triggered when a new message is received
// from a client. It accepts `anydata` as the function argument. The received data
// will be converted to the data type stated as the function argument.
remote function onMessage(websocket:Caller caller, string chatMessage) returns error? {
if (self.userType === "PUBLISHER") {
return self.broadCast(chatMessage);
}
}
remote isolated function onClose(websocket:Caller caller) {
if (self.userType === "PUBLISHER") {
lock {
string[] publishersIds = publisherList.'map(n => n.getConnectionId());
int index = <int>publishersIds.indexOf(caller.getConnectionId());
_ = publisherList.remove(index);
}
}
if (self.userType === "SUBSCRIBER") {
lock {
string[] subscriberIds = subscriberList.'map(n => n.getConnectionId());
int index = <int>subscriberIds.indexOf(caller.getConnectionId());
_ = subscriberList.remove(index);
}
}
}
isolated function broadCast(string msg) returns error? {
lock {
foreach websocket:Caller item in subscriberList {
if (self.multipleTopics.length() == 1) { //check publisher single topic
if (item.getAttribute("isMultipleTopics") == true) { //single topic pub -> multiple topic sub
string[] subscriberTopics = check item.getAttribute("multipleTopics").ensureType();
foreach string topic in subscriberTopics {
if (self.multipleTopics[0] === topic) {
websocket:Error? writeTextMessage = item->writeMessage(msg);
if writeTextMessage is error {
io:println("Error in Bradcasting:", writeTextMessage);
}
}
}
}
else { //single topic pub -> single topic sub
if (item.getAttribute("topic") === self.multipleTopics[0]) {
websocket:Error? writeTextMessage = item->writeMessage(msg);
if writeTextMessage is error {
io:println("Error in Broadcasting:", writeTextMessage);
}
}
}
}
else if (self.multipleTopics.length() > 1) { //check publishers multiple topics
if (item.getAttribute("isMultipleTopics") == true) { //multi topic pub -> multi topic pub
string[] subscriberTopics = check item.getAttribute("multipleTopics").ensureType();
foreach string subTopic in subscriberTopics {
boolean isFound = self.multipleTopics.some(n => n === subTopic);
if (isFound) {
websocket:Error? writeTextMessage = item->writeMessage(msg);
if writeTextMessage is error {
io:println("Error in Broadcasting:", writeTextMessage);
}
}
}
}
else { //multi topic pub -> single topic sub
string subTopic = check item.getAttribute("topic").ensureType();
int? index = self.multipleTopics.indexOf(subTopic);
if index is int {
websocket:Error? writeTextMessage = item->writeMessage(msg);
if writeTextMessage is error {
io:println("Error in Bradcasting:", writeTextMessage);
}
}
}
}
}
}
}
}