Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save madushadhanushka/444595c1976f84f2f5638c7db344d5cb to your computer and use it in GitHub Desktop.
Save madushadhanushka/444595c1976f84f2f5638c7db344d5cb to your computer and use it in GitHub Desktop.
Websocket ballerina
import ballerina/http;
import ballerina/io;
import ballerina/websocket;
map<websocket:Caller> clientConnectionMap = {};
string clientName = "";
const string NAME = "NAME";
@websocket:ServiceConfig {
subProtocols: ["xml"],
idleTimeout: 120
}
service /chat on new websocket:Listener(9091) {
resource function get [string name](http:Request req)
returns websocket:Service|websocket:Error {
clientName = name;
return service object websocket:Service {
remote function onOpen(websocket:Caller caller) {
clientConnectionMap[caller.getConnectionId()] = <@untainted>caller;
caller.setAttribute(NAME, clientName);
broadcastMessage(caller.getAttribute(NAME).toString() + " join the conversation");
}
remote function onTextMessage(websocket:Caller caller, string text) {
broadcastMessage(caller.getAttribute(NAME).toString() + ": " + text);
}
remote function onClose(websocket:Caller caller, int statusCode, string reason) {
_ = clientConnectionMap.remove(caller.getConnectionId());
broadcastMessage(caller.getAttribute(NAME).toString() + " left the conversation");
}
};
}
}
service /web on new http:Listener(9090) {
resource function get .(http:Caller caller,http:Request request) returns error? {
http:Response response = new;
response.setFileAsPayload("index.html", contentType = "text/html");
check caller->respond(response);
}
}
function broadcastMessage(string message) {
foreach var con in clientConnectionMap {
var err = con->writeTextMessage(message);
if (err is websocket:Error) {
io:println("Error while sending message:");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment