本文介绍了Actor中的WebSocket.acceptWithActor和@Inject()(播放2.5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WebSocket.acceptWithActor实例化一个新的Akka演员,而无需使用Guice.

WebSocket.acceptWithActor instantiates a new Akka actor without making use of Guice.

在Play 2.4中,仍然可以通过导入play.api.Play.current为演员使用喷射器.

With Play 2.4, using the injector for my actor was still possible by importing play.api.Play.current.

ReactiveMongo文档中的摘录:

import scala.concurrent.Future

import play.api.Play.current // should be deprecated in favor of DI
import play.api.libs.concurrent.Execution.Implicits.defaultContext

import play.modules.reactivemongo.ReactiveMongoApi
import play.modules.reactivemongo.json.collection.JSONCollection

object Foo {
  lazy val reactiveMongoApi = current.injector.instanceOf[ReactiveMongoApi]

  def collection(name: String): Future[JSONCollection] =
    reactiveMongoApi.database.map(_.collection[JSONCollection](name))
}

但是在Play 2.5中,不推荐使用play.api.Play.current.如何仍然将ReactiveMongoApi注入演员中?在演员中使用ReactiveMongoApi实例的推荐方式是什么?

But in Play 2.5, play.api.Play.current is deprecated. How can I still inject ReactiveMongoApi in my actor? What is the recommended way of using an instance of ReactiveMongoApi in my actor?

这是我的与Play 2.4兼容的代码,因为我的自定义演员类ClientActor可以通过current.injector.instanceOf[ReactiveMongoApi]访问ReactiveMongoApi:

Here is my code which works with Play 2.4 because my custom actor class ClientActor has access to ReactiveMongoApi through current.injector.instanceOf[ReactiveMongoApi]:

@Singleton
class Application @Inject() (system: ActorSystem) extends Controller {

  val midiDiscoveryActor = system.actorOf(MidiDiscoveryActor.props, "midi-discovery-actor")
  val midiActor = system.actorOf(MidiActor.props(midiDiscoveryActor), "midi-actor")

  def index(page: String) = Action {
    Ok(views.html.index(page))
  }

  def bidirectional = WebSocket.acceptWithActor[JsValue, JsValue] { request => out =>
    ClientActor.props(out, midiActor, midiDiscoveryActor)
  }

}

推荐答案

我认为这是不可能的.引用 James Roper :

I don't think this is possible. Quoting James Roper:

class MyController @Inject() (myDep: MyDep) extends Controller {
  def socket = WebSocket.acceptWithActor[String, String] { request => out =>
    MyWebSocketActor.props(out, myDep)
  }
}

这篇关于Actor中的WebSocket.acceptWithActor和@Inject()(播放2.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:56