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 ...
Mastering play framework for scala
By :
Mastering play framework for scala
By:
Overview of this book
Table of Contents (21 chapters)
Mastering Play Framework for Scala
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
Getting Started with Play
Defining Actions
Building Routes
Exploring Views
Working with Data
Reactive Data Streams
Playing with Globals
WebSockets and Actors
Testing
Debugging and Logging
Web Services and Authentication
Play in Production
Writing Play Plugins
Index
Customer Reviews