250
Javalin 0.5.1 - WebSockets - Javalin: Simple REST APIs for Java and Kotlin
source link: https://javalin.io/news/2017/09/22/javalin-0.5.1-released.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Javalin 0.5.1 released
WebSockets (Sep 22, 2017)
What's Javalin? Javalin is a very lightweight Java and Kotlin web framework which focuses on simplicity and Java/Kotlin interoperability.
Read more on our landing page.
Functional WebSockets
There is only one change in this release, but it’s a big one. Version 0.5.1 introduces a WebSocket API, built on top of Jetty.
WebSockets can be enabled in three different ways:
Lambda approach
- Kotlin
app.ws("/websocket", ws -> {
ws.onConnect(session -> System.out.println("Connected"));
ws.onMessage((session, message) -> {
System.out.println("Received: " + message);
session.getRemote().sendString("Echo: " + message);
});
ws.onClose((session, statusCode, reason) -> System.out.println("Closed"));
ws.onError((session, throwable) -> System.out.println("Errored"));
});
Annotated class
You can pass an annotated class to the ws()
function:
app.ws("/websocket", WebSocketClass.class);
Annotation API can be found on Jetty’s docs page
WebSocket object
You can pass any object that fulfills Jetty’s requirements (annotated/implementing WebSocketListener
, etc):
app.ws("/websocket", new WebSocketObject());
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK