aboutsummaryrefslogtreecommitdiff
path: root/server/app/controllers/HomeController.scala
blob: 7f5e5962a337b6226091b649e88d5b6e2d1642f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package controllers

import akka.actor.{ActorRef, ActorSystem, Props}
import akka.stream.Materializer
import chat._
import javax.inject._
import play.api.libs.streams.ActorFlow
import play.api.mvc._
import play.api.mvc.WebSocket.MessageFlowTransformer
import upickle.default._

/**
 * This controller creates an `Action` to handle HTTP requests to the
 * application's home page.
 */
class HomeController @Inject() (
  implicit system: ActorSystem,
  mat: Materializer
) extends Controller {

  lazy val room: ActorRef = system.actorOf(Props(classOf[RoomActor]))

  /**
   * Create an Action to render an HTML page.
   *
   * The configuration in the `routes` file means that this method
   * will be called when the application receives a `GET` request with
   * a path of `/`.
   */
  def index = Action { implicit request =>
    Ok(views.html.index())
  }

  def chat(uid: String) = Action { implicit request =>
    Ok(views.html.chat(uid))
  }

  def socket(uid: String) = WebSocket.accept[Command, Event] { request =>
    ActorFlow.actorRef(out => Props(classOf[ClientActor], uid, room, out))
  }

  implicit val transformer = MessageFlowTransformer.stringMessageFlowTransformer.map(
    in => read[Command](in),
    (out: Event) => write(out)
  )

}