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

Closing a WebSocket


When the WebSocket is closed, Play automatically stops the actor bound to it. This binding works in two ways: the WebSocket connection is closed when the underlying actor is killed. If there is a need to free any resources once the connection is closed, we can do so by overriding the actor's postStop method. In our example, we have initialized a DBActor within WebSocketChannel. We will need to ensure that it's killed once the WebSocket is closed, since each connection to the WebSocket will lead to the initialization of a DBActor. We can do so by sending it a poison pill, as shown here:

override def postStop() = {
  backend ! PoisonPill
}

Using FrameFormatter

Suppose that an incoming JSON has the same fields for every request, instead of parsing it every time; we can define an equivalent class in this way:

case class WebsocketRequest(reqType:String, message:String)

Now, we can define our WebSocket to translate the JSON message to a WebSocketRequest automatically. This is possible...