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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With