10235
playでactorから接続しているクライアントにbroadcastするにはどうしたらよいですか？

playでwebsocketを使うときの参考になるサイトがあったらぜひ教えて下さい。

```

    //Global.scala
    object Global extends GlobalSettings {
    
      override def onStart(app: Application) {
        val system = ActorSystem("system")
        val hello = system.actorOf(Props[HelloActor], "HelloActor")
        hello ! "world"
      }
    
    }
    
    //HelloActor.scala
    class HelloActor extends Actor {
      def receive = {
        case "world" => helloWorld
      }
    
      def helloWorld = {
        val (out, channel) = Concurrent.broadcast[String]
        channel.push("HelloWorld!!")
      }
    }
    
```
CC BY-SA 3.0
2015-05-11T01:28:48.350
10016
2015-05-15T21:21:26.470
2015-05-13T11:12:51.537
8355
8355
question
scala playframework akka
actorからwebsocketのbroadcastをするには
<https://www.playframework.com/documentation/2.3.x/ScalaWebSockets#Handling-
WebSockets-with-iteratees>

上のドキュメントにあるように下記のような方法でどうでしょうか？

```

    import play.api.mvc._
    import play.api.libs.iteratee._
    import play.api.libs.concurrent.Execution.Implicits.defaultContext
    
    def socket =  WebSocket.using[String] { request =>
    
      // Concurrent.broadcast returns (Enumerator, Concurrent.Channel)
      val (out, channel) = Concurrent.broadcast[String]
    
      // log the message to stdout and send response back to client
      val in = Iteratee.foreach[String] {
        msg => println(msg)
          // the Enumerator returned by Concurrent.broadcast subscribes to the channel and will
          // receive the pushed messages
          channel push("I received your message: " + msg)
      }
      (in,out)
    }
    
```
CC BY-SA 3.0
2015-05-11T03:55:22.960
10023
2015-05-11T03:55:22.960
2432
10016
answer
ControllerにBroadcast用のデータのレシーバーを用意して、データを受信したらBroadcastするようにして解決しました。

```

    //Application.scala
    object Application extends Controller {
    
      var channel: MutableList[Concurrent.Channel[String]] = MutableList()
    
      def socket = WebSocket.using[String] { request =>
        val in = Iteratee.ignore[String]
        val out = Concurrent.unicast[String]{ c => channel += c }
        (in, out)
      }
    
      def reciever = Action { request =>
        channel.foreach(_.push(request.body.asJson.toString))
        Ok("success!")
      }
    }
    
    
    //HelloActor.scala
    class HelloActor extends Actor {
        def receive = {
            case "Hello" => WS.url(url+"/product/info").withHeaders("Content-Type" -> "text/plain").post("Hello!")
            case _ =>
        }
    }
    
```
CC BY-SA 3.0
2015-05-15T12:06:09.657
10235
2015-05-15T21:21:26.470
2015-05-15T21:21:26.470
8355
8355
10016
answer