Skip to content

Instantly share code, notes, and snippets.

@duanebester
Created December 9, 2019 00:40
Show Gist options
  • Save duanebester/2d1631c9495b740478e150c08837b3a0 to your computer and use it in GitHub Desktop.
Save duanebester/2d1631c9495b740478e150c08837b3a0 to your computer and use it in GitHub Desktop.
Akka WebSocket Example
def greeter: Flow[Message, Message, Any] =
Flow[Message].mapConcat {
case tm: TextMessage =>
TextMessage(Source.single("Hello ") ++ tm.textStream ++ Source.single("!")) :: Nil
case bm: BinaryMessage =>
// ignore binary messages but drain content to avoid the stream being clogged
bm.dataStream.runWith(Sink.ignore)
Nil
}
val websocketRoute =
path("greeter") {
handleWebSocketMessages(greeter)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment