Troubleshooting
What is the equivalent of interrupting
Actions
inGlobalSettings
forWebSockets
? What if we want to refuse a WebSocket connection when certain headers are missing? Something similar to the following code snippet didn't work as expected:override def onRouteRequest(request: RequestHeader): Option[Handler] = { if(request.path.startsWith("/ws")){ Option(controllers.Default.error) } else super.onRouteRequest(request) }
Interrupting WebSocket from the global object does not work as it does for Actions. However, there are other means of doing so: by using the
tryAccept
andtryAcceptWithActor
methods. A WebSocket definition can be replaced by the following code:def wsWithHeader = WebSocket.tryAccept[String] { rh => Future.successful(rh.headers.get("token") match { case Some(x) => var channel: Concurrent.Channel[String] = null val out = Concurrent.unicast[String] { ch => channel = ch ...