Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Play 2.5.x equivalent to acceptWithActor[String, JsValue]?

Before I migrated to Play 2.5.x I used WebSocket.acceptWithActor frequently. Now I can't get my web sockets to stay open when using different input and output, in my case, input is String, output is JsValue.

My receiver before Play 2.5.x:

object Application extends Controller {
    def myWebSocket = WebSocket.acceptWithActor[String, JsValue] { request =>
        out => MyActor.props(out)
}

My receiver in Play 2.5.x:

@Singleton
class Application @Inject() (implicit system: ActorSystem, materializer: Materializer) extends Controller {
  implicit val messageFlowTransformer = 
                  MessageFlowTransformer.jsonMessageFlowTransformer[String, JsValue]
  def myWebSocket = WebSocket.accept[String, JsValue] { request =>
    ActorFlow.actorRef(out => MyActor.props(out))
  }
}

In my actor preStart is called immediately followed by postStop, so this is obviously incorrect, but I can't seem to find any solution in the documentation (https://www.playframework.com/documentation/2.5.x/ScalaWebSockets). If I use WebSocket.accept[String, String] the socket stays open.

What am I doing wrong?

like image 818
Robert F Avatar asked Oct 03 '16 14:10

Robert F


1 Answers

I've found a workarround

My controller:

    ActorFlow.actorRef[String, JsValue](out =>
      MyActor.props(
         out,
         ...
      )
    ).map(_.toString)

With this you shouln't define a custom messageflowtransformer and at actor level you can still receive strings and return JsValues.

like image 106
SergiGP Avatar answered Nov 15 '22 13:11

SergiGP