Last active
August 9, 2020 17:07
-
-
Save nsadeh/37c5dde57841fd61c788387c5fae4790 to your computer and use it in GitHub Desktop.
Basic Web Server with an Affirming WebSockets endpoint
This file contains 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 akka.http.scaladsl.server.Directives._ | |
import akka.http.scaladsl.server.Route | |
import akka.http.scaladsl.Http | |
import akka.stream.ActorMaterializer | |
import scala.io.StdIn | |
import akka.stream.scaladsl.Flow | |
import akka.http.scaladsl.model.ws.{ Message, TextMessage } | |
object WebServer extends App { | |
// implicit values required by the server machinery | |
implicit val actorSystem = akka.actor.ActorSystem("messaging-actorsystem") | |
implicit val materializer = ActorMaterializer() | |
implicit val executionContext = actorSystem.dispatcher | |
// define a basic route ("/") that returns a welcoming message | |
def helloRoute: Route = pathEndOrSingleSlash { | |
complete("Welcome to messaging service") | |
} | |
// create WebSocket route | |
def affirmRoute = path("affirm") { | |
handleWebSocketMessages( | |
Flow[Message].collect { | |
case TextMessage.Strict(text) => TextMessage("You said " + text) | |
} | |
) | |
} | |
// bind the route using HTTP to the server address and port | |
val binding = Http().bindAndHandle(helloRoute ~ affirmRoute, "localhost", 8080) | |
println("Server running...") | |
// kill the server with input | |
StdIn.readLine() | |
binding.flatMap(_.unbind()).onComplete(_ => actorSystem.terminate()) | |
println("Server is shut down") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment