Created
March 12, 2018 10:11
-
-
Save riyafa/f1beafb8ff6ff098742e0f990c3e7c39 to your computer and use it in GitHub Desktop.
Websocket upgrade from HTTP
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.net.http; | |
| import ballerina.io; | |
| import ballerina.runtime; | |
| endpoint<http:Service> passthruEP { | |
| port:9090 | |
| } | |
| endpoint< http:Client> backendClientEP { | |
| serviceUri: "http://www.mocky.io" | |
| } | |
| @http:serviceConfig { | |
| basePath:"/passthru", | |
| endpoints:[passthruEP], | |
| webSocketUpgrade: { | |
| upgradePath: "/ws", | |
| upgradeService: typeof hello | |
| } | |
| } | |
| service<http:Service> passthrough { | |
| @http:resourceConfig { | |
| methods:["GET"], | |
| path:"/" | |
| } | |
| resource passthru (http:ServerConnector conn, http:Request request) { | |
| var httpClient = backendClientEP.getConnector(); | |
| var resp, err = httpClient -> get("/v2/5aa3a485310000461026e292", request); | |
| _ = conn->forward(resp); | |
| } | |
| } | |
| @http:webSocketServiceConfig { | |
| idleTimeoutInSeconds:1000 | |
| } | |
| service<http:WebSocketService> hello { | |
| string ping = "ping"; | |
| blob pingData = ping.toBlob("UTF-8"); | |
| resource onOpen (http:WebSocketConnector conn) { | |
| io:println("\nNew client connected"); | |
| } | |
| resource onTextMessage (http:WebSocketConnector conn, http:TextFrame frame) { | |
| io:println("\ntext message: " + frame.text + " & is final fragment: " + frame.isFinalFragment); | |
| string text = frame.text; | |
| if (text == "ping") { | |
| io:println("Pinging..."); | |
| conn->ping(pingData); | |
| } else if (text == "closeMe") { | |
| conn->closeConnection(1001, "You asked me to close connection"); | |
| } else { | |
| conn->pushText("You said: " + frame.text); | |
| } | |
| } | |
| resource onBinaryMessage(http:WebSocketConnector conn, http:BinaryFrame frame) { | |
| io:println("\nNew binary message received"); | |
| blob b = frame.data; | |
| io:println("UTF-8 decoded binary message: " + b.toString("UTF-8")); | |
| conn->pushBinary(b); | |
| } | |
| resource onPing(http:WebSocketConnector conn, http:PingFrame frame) { | |
| conn->pong(frame.data); | |
| } | |
| resource onPong(http:WebSocketConnector conn, http:PongFrame frame) { | |
| io:println("Pong received"); | |
| } | |
| resource onIdleTimeout(http:WebSocketConnector conn) { | |
| io:println("\nReached idle timeout"); | |
| io:println("Closing connection " ); | |
| conn->closeConnection(1001, "Connection timeout"); | |
| } | |
| resource onClose(http:WebSocketConnector conn, http:CloseFrame closeFrame) { | |
| io:println("\nClient left with status code " + closeFrame.statusCode + " because " + closeFrame.reason); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment