Last active
October 14, 2019 13:16
-
-
Save riyafa/f5fd2e7c8e04a5858244b0cee182517f to your computer and use it in GitHub Desktop.
A secure WebSocket client using Ballerina
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/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