Created
January 3, 2021 16:11
-
-
Save kalaiyarasiganeshalingam/3079786c3a5a3e0f1cd57b2a331847d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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