Play framework 2.5.0 Websockets example [closed]

2019-03-27 15:01发布

Play framework 2.5.0 Websockets example.

in play 2.5.0 websockets code is changed to support akka streams but I'm not able find a sample code to use it.

1条回答
Rolldiameter
2楼-- · 2019-03-27 15:15

This will be properly documented in 2.5.1 as you can see here: https://github.com/playframework/playframework/issues/5057


In the meantime you can take a look at the Migration Guide which has a part on Streams: https://www.playframework.com/documentation/2.5.x/StreamsMigration25#Migrating-WebSockets-%28WebSocket%29

You will notice that the important part is the WebSocket.MappedWebSocketAcceptor<In,Out> class. You use this one to tell Play how to convert Message frames to your own types - like String, Json, etc.

Fortunately the Java API of Play provides some predefined implementations. Let's say you want to handle WebSocket connections which exchange JSON data. Then you would use WebSocket.Json (https://github.com/playframework/playframework/blob/master/framework/src/play/src/main/java/play/mvc/WebSocket.java#L71)

return WebSocket.Json.accept(requestHeader -> {
  // returns a Flow<JsonNode, JsonNode, ?>
})
查看更多
登录 后发表回答