Book Image

Mastering play framework for scala

By : Shiti Saxena
Book Image

Mastering play framework for scala

By: Shiti Saxena

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
1
Getting Started with Play
Index

Troubleshooting


  • What is the equivalent of interrupting Actions in GlobalSettings for WebSockets? 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 and tryAcceptWithActor 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
       ...