Skip to content

Instantly share code, notes, and snippets.

@riyafa
Last active October 14, 2019 13:16
Show Gist options
  • Save riyafa/f5fd2e7c8e04a5858244b0cee182517f to your computer and use it in GitHub Desktop.
Save riyafa/f5fd2e7c8e04a5858244b0cee182517f to your computer and use it in GitHub Desktop.
A secure WebSocket client using Ballerina
import ballerina/io;
import ballerina/http;
string ping = "ping";
byte[] pingData = ping.toBytes();
public function main() {
http:WebSocketClient wsClientEp = new("wss://echo.websocket.org", config={secureSocket: {
trustedCertFile:"/home/riyafa/websocketorg.crt"
},
callbackService: ClientService
});
checkpanic wsClientEp->ping(pingData);
}
service ClientService =@http:WebSocketServiceConfig {} service {
//This resource is triggered when a new text frame is received from the remote backend.
resource function onText(http:WebSocketClient caller, string text, boolean finalFrame) {
io:println(text);
}
resource function onPong(http:WebSocketClient caller, byte[] data){
io:println("pong");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment