Created
April 2, 2015 14:56
-
-
Save dat-vikash/a2748f0da202387b5b4a to your computer and use it in GitHub Desktop.
Play 2.2.x Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Application extends Controller { | |
/* Each WebSocket connection state is managed by an Agent actor. | |
A new actor is created for each WebSocket, and is killed when the socket is closed. | |
For each play actor agent, a unique WebSocket Client Worker actor is created to process WS events via the WSManager Actor. | |
*/ | |
def websocketManager(deviceId: String) = WebSocket.async[JsValue] | |
{ | |
request => | |
// instantiate an actor to hold web socket | |
val webSocketWorker = Akka.system.actorOf(Props(new WSClientVisitor(deviceId),name=deviceId) | |
//specify a timeout for the registration request | |
implicit val timeout = Timeout(Duration(2,"seconds")) | |
//register device | |
(webSocketWorker ? RegisterSocket).map { | |
//establish connection with wsClient Worker, which will process messages to our socket | |
case Connected(out, myWorker) => { | |
// acknowledge connection | |
val in = Iteratee.foreach[JsValue] { | |
event => myWorker ! Message(event) | |
}.mapDone(_ => myWorker ! Logger.info(s" Received Websocket FIN command from device: $deviceId")) | |
(in,out) | |
} | |
case NotConnected(out) => { | |
(Iteratee.ignore[JsValue],out) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment