I am playing around with WebSockets in PlayFramework 2.2. I would like to do some checks on the initial request and possibly return an appropriate HTTP status. In principle it would look like something like this, asked in this question:
def ws(username: String) = {
if (username == "asoliman")
Action { request =>
Forbidden("Soliman is not allowed here")
}
else
WebSocket.using[String] { request =>
val in = Iteratee.foreach[String]( s => println("got: " + s)).mapDone(_ => println("Disconnected"))
val out = Enumerator[String]("Ahmed", "Mohamed", "Ibrahim").andThen(Enumerator.enumInput(Input.EOF))
(in, out)
}
}
As noted, this is not possible as the WebSocket's using and async need to return a Tuple2[Iteratee, Enumerator].
Is there a recommended approach to this? Or, is there a way to send Websocket's Status Codes?
UPDATE 7 October 2015:
In newer PlayFramework versions, it is possible to reject a connection and therefore return, say, a Forbidden status. Check the documentation here: https://www.playframework.com/documentation/2.4.x/ScalaWebSockets
ORIGINAL ANSWER:
Answer is, it is not possible at the moment with PlayFramework 2.2. Regular HTTP statuses are not viable as the response needs to be a WebSocket (via using/async), and WebSocket statuses are not implemented. I filed an issue about it in their repo, we need to wait for future releases - or contribute to make it happen :-)
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