Reputation: 6431
I'm attempting to send a message to websocket instantly after the connection is estabilished. But I don't know, where to put the code with the message send.
def ws = WebSocket.async[String] {
val (out, channel) = {
Concurrent.broadcast[String]
}
channel push "message"
request =>
(in, out)
}
I can see, that when I send the message to the channel before the ws
method returns, the web client doesn't get the message. If I manage to run the code channel push "message"
after the method ws
returns, it works fine and I receive the message at the browser client. I can delay it by wrapping the channel push "message"
in Future{}
, starting with some Thread.sleep()
.
But I hope, there some reliable, non-blocking solution...
Upvotes: 1
Views: 249
Reputation: 7373
I see from the docs that async
expects a lambda returning a Promise[stuff]
.
Supposing that (in, out)
gets implicitly converted to said Promise you could try to explicitly create the promise and register a listener like
def ws = WebSocket.async[String] {
val (out, channel) = {
Concurrent.broadcast[String]
}
request =>
val p = Promise((in, out))
p.onRedeem(_ => channel push "message")
p
}
I'm no Play! expert here, so I'm just guessing.
Upvotes: 2