在 Play + Scala 中构建 websocket 时出现 'unable to resolve overloaded method actorRef' 错误
Getting 'unable to resolve overloaded method actorRef' error when building websockets in Play + Scala
我正在尝试按照 Play 网站 here 上的说明设置基本的 websocket。但是,当尝试使用以下代码时出现错误:
Cannot resolve overloaded method 'actorRef'
代码如下:
package controllers
import play.api.mvc._
import javax.inject._
import akka.actor._
import akka.stream.Materializer
import play.libs.streams.ActorFlow
@Singleton
class HomeController @Inject()(cc: ControllerComponents)(implicit system: ActorSystem, mat: Materializer) extends AbstractController(cc) {
def ws = WebSocket.accept[String, String] { request =>
ActorFlow.actorRef { out =>
MyWebSocketActor.props(out)
}
}
}
object MyWebSocketActor {
def props(out: ActorRef) = Props(new MyWebSocketActor(out))
}
class MyWebSocketActor(out: ActorRef) extends Actor {
def receive = {
case msg: String =>
out ! ("I received your message: " + msg)
}
}
设置控制器接受 websocket 连接的正确方法是什么?
好的,事实证明我在导入时错误地使用了 Java API 而不是 Scala。我更改了以下内容:
import play.libs.streams.ActorFlow
至
import play.api.libs.streams.ActorFlow
这解决了问题!
我正在尝试按照 Play 网站 here 上的说明设置基本的 websocket。但是,当尝试使用以下代码时出现错误:
Cannot resolve overloaded method 'actorRef'
代码如下:
package controllers
import play.api.mvc._
import javax.inject._
import akka.actor._
import akka.stream.Materializer
import play.libs.streams.ActorFlow
@Singleton
class HomeController @Inject()(cc: ControllerComponents)(implicit system: ActorSystem, mat: Materializer) extends AbstractController(cc) {
def ws = WebSocket.accept[String, String] { request =>
ActorFlow.actorRef { out =>
MyWebSocketActor.props(out)
}
}
}
object MyWebSocketActor {
def props(out: ActorRef) = Props(new MyWebSocketActor(out))
}
class MyWebSocketActor(out: ActorRef) extends Actor {
def receive = {
case msg: String =>
out ! ("I received your message: " + msg)
}
}
设置控制器接受 websocket 连接的正确方法是什么?
好的,事实证明我在导入时错误地使用了 Java API 而不是 Scala。我更改了以下内容:
import play.libs.streams.ActorFlow
至
import play.api.libs.streams.ActorFlow
这解决了问题!