Skip to content

Instantly share code, notes, and snippets.

@kalaiyarasiganeshalingam
Created January 3, 2021 16:11
Show Gist options
  • Select an option

  • Save kalaiyarasiganeshalingam/3079786c3a5a3e0f1cd57b2a331847d3 to your computer and use it in GitHub Desktop.

Select an option

Save kalaiyarasiganeshalingam/3079786c3a5a3e0f1cd57b2a331847d3 to your computer and use it in GitHub Desktop.
import ballerina/http;
import ballerina/log;
import ballerina/io;
// Annotations specifying the webSocket service.
@http:WebSocketServiceConfig {
path: "/websocket"
}
// Define the backend service with port 15300.
service server on new http:Listener(9090) {
resource function onOpen(http:WebSocketCaller caller) {
log:printInfo("WebSocket client connect wih the server. The Connection ID: "
+ caller.getConnectionId());
checkpanic caller->pushText("Hi");
}
// This resource gets invoked when a server is receiving a text message from the client
resource function onText(http:WebSocketCaller caller, string text, boolean finalFrame) {
io:println(text);
var err = caller->pushText(text, finalFrame);
if (err is http:WebSocketError) {
log:printError("Error occurred when sending text message", err);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment