ChatRoom.scala 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package models
  2. import akka.actor._
  3. import scala.concurrent.duration._
  4. import scala.language.postfixOps
  5. import play.api._
  6. import play.api.libs.json._
  7. import play.api.libs.iteratee._
  8. import play.api.libs.concurrent._
  9. import akka.util.Timeout
  10. import akka.pattern.ask
  11. import play.api.Play.current
  12. import play.api.libs.concurrent.Execution.Implicits._
  13. object ChatRoom {
  14. implicit val timeout = Timeout(1 second)
  15. lazy val roomMonitor = Akka.system.actorOf(Props[ChatRoom])
  16. var chatRooms = Map.empty[String, ActorRef]
  17. def closeRoom(room: String) = {
  18. chatRooms -= room
  19. }
  20. def join(room: String, username:String):scala.concurrent.Future[(Iteratee[JsValue,_],Enumerator[JsValue])] = {
  21. if(!chatRooms.keySet(room)) {
  22. chatRooms += (room -> Akka.system.actorOf(Props(classOf[ChatRoom], room)))
  23. }
  24. val actor = chatRooms(room)
  25. (actor ? Join(username)).map {
  26. case Connected(enumerator) =>
  27. // Create an Iteratee to consume the feed
  28. val iteratee = Iteratee.foreach[JsValue] { event =>
  29. actor ! Talk(username, (event \ "text").as[String])
  30. }.map { _ =>
  31. actor ! Quit(username)
  32. }
  33. (iteratee,enumerator)
  34. case CannotConnect(error) =>
  35. // Connection error
  36. // A finished Iteratee sending EOF
  37. val iteratee = Done[JsValue,Unit]((),Input.EOF)
  38. // Send an error and close the socket
  39. val enumerator = Enumerator[JsValue](JsObject(Seq("error" -> JsString(error)))).andThen(Enumerator.enumInput(Input.EOF))
  40. (iteratee,enumerator)
  41. }
  42. }
  43. }
  44. class ChatRoom(name: String) extends Actor {
  45. val tabooGame = Akka.system.actorOf(Props(classOf[TabooGame], self))
  46. var members = Map.empty[String, Concurrent.Channel[JsValue]]
  47. val (chatEnumerator, chatChannel) = Concurrent.broadcast[JsValue]
  48. def receive = {
  49. case Join(username) => {
  50. if(members.contains(username)) {
  51. sender ! CannotConnect("This username is already used")
  52. } else {
  53. val (personalEnumerator, personalChannel) = Concurrent.broadcast[JsValue]
  54. members = members + (username -> personalChannel)
  55. sender ! Connected(chatEnumerator.interleave(personalEnumerator))
  56. tabooGame ! Join(username)
  57. }
  58. }
  59. case Talk(username, "/ping") => {
  60. self ! Tell(username, Json.obj(
  61. "kind" -> "pong"
  62. ))
  63. }
  64. case Talk(username, text) => {
  65. if(!text.startsWith("/")) {
  66. self ! Announce(Json.obj(
  67. "kind" -> "talk",
  68. "user" -> username,
  69. "message" -> text
  70. ))
  71. }
  72. tabooGame ! Talk(username, text)
  73. }
  74. case Announce(message) => {
  75. chatChannel.push(message)
  76. }
  77. case Tell(username, message) => {
  78. members(username).push(message)
  79. }
  80. case Quit(username) => {
  81. members = members - username
  82. if(members.isEmpty) {
  83. ChatRoom.closeRoom(name)
  84. context.stop(tabooGame)
  85. context.stop(self)
  86. }
  87. else {
  88. tabooGame ! Quit(username)
  89. }
  90. }
  91. }
  92. }
  93. case class Join(username: String)
  94. case class Quit(username: String)
  95. case class Talk(username: String, text: String)
  96. case class Announce(value: JsValue)
  97. case class Tell(username: String, value: JsValue)
  98. case class Connected(enumerator:Enumerator[JsValue])
  99. case class CannotConnect(msg: String)