Skip to content

Instantly share code, notes, and snippets.

@riyafa
Last active October 14, 2019 14:05
Show Gist options
  • Select an option

  • Save riyafa/b58b4e6d424f65634179e1cd7704585f to your computer and use it in GitHub Desktop.

Select an option

Save riyafa/b58b4e6d424f65634179e1cd7704585f to your computer and use it in GitHub Desktop.
Attach a Ballerina WebSocket service to a http:Listener.
import ballerina/http;
import ballerina/io;
listener http:Listener ep = new (9090);
service wsService = @http:WebSocketServiceConfig {} service {
resource function onText(http:WebSocketCaller wsEp, string text) {
io:println("Sever Received: " + text);
}
resource function onClose(http:WebSocketCaller wsEp, int statusCode, string reason) {
io:println("Connection closed");
}
};
public function main() {
var err = ep.__attach(wsService);
if (err is error) {
io:println("error in attaching service to Listener", err);
}
}
@riyafa
Copy link
Author

riyafa commented Oct 14, 2019

Call the service as var ws = new WebSocket("ws://localhost:9090"); using a js client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment