Skip to content

Instantly share code, notes, and snippets.

@dat-vikash
dat-vikash / play2.2.0_websockets
Created October 11, 2013 15:26
Play2.2 basic websockets example
//This shows an updated websocket example for play2.2.0 utilizing Concurrent.broadcast vs Enumerator.imperative, which
// is now deprecated.
object Application extends Controller {
def index = WebSocket.using[String] { request =>
//Concurernt.broadcast returns (Enumerator, Concurrent.Channel)
val (out,channel) = Concurrent.broadcast[String]
// Create the Global class in your /app folder root package:
import play.api.{GlobalSettings, Play}
import play.api.Play.current
import play.api.mvc._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
/**
@dat-vikash
dat-vikash / gist:d887af0fef06df61ceef
Created March 31, 2015 16:25
SIM Distribution: fitSenatorsBasedOnRarity
[1] def fitSenatorsBasedOnRarity(committeesWithIssueConfs: Map[Committee, mutable.Queue[String]], senators: Set[Senator], party: String) =
{
[2] var takenSenators: mutable.Set[Senator] = senators.to[mutable.Set]
[3] committeesWithIssueConfs.foreach(committee =>
committee._2.foreach(issue => {
// get a senator from each faction
[4] val tmpSenatorDem = takenSenators.find(s => s.faction.name.equalsIgnoreCase(party) && doesMatchPositionRule(issue, s))
[5] tmpSenatorDem match {
case None => // do nothing
case Some(senator) => {
@dat-vikash
dat-vikash / gist:6f21ef2721147b09be55
Created March 31, 2015 16:35
SIM Distribution: defineSenatorTrack
[1] def defineSenatorTrack() =
{
// for every committee
[2] committeeIssueSenatorMap.foreach( committee => {
committee._2.foreach(issueConf => {
issueConf._2.foreach( senator => {
[3] val max = committee._2.foldLeft(0)( (result, current) => result + current._2.filter(_.faction.name.equalsIgnoreCase(senator.faction.name)).size) / 2
// does the senator fit in subcommittee 1
[4] if(doesSenatorFitInSubCommittee(senator.faction.name, max, senator, committee._1.name,SUBCOMMITTEE_1_KEY)) {
// add
/* This trait allows websocket classes to push messages to client via a concurrent channel */
trait WebSocketChannel
{
//instantiate an Enumerator and Channel
val (out,channel) = Concurrent.broadcast[JsValue]
// method to allow pushing of data up the channel
def push(data: JsValue) = channel.push(data)
def cleanSocketResources() = channel.end()
}
@dat-vikash
dat-vikash / gist:2ae6abfc9e6cf70c7660
Created April 2, 2015 14:52
Play 2.2.x WebSocketChannel trait
/* This trait allows websocket classes to push messages to client via a concurrent channel */
trait WebSocketChannel
{
//instantiate an Enumerator and Channel
val (out,channel) = Concurrent.broadcast[JsValue]
// method to allow pushing of data up the channel
def push(data: JsValue) = channel.push(data)
def cleanSocketResources() = channel.end()
}
@dat-vikash
dat-vikash / gist:ae2d3183e28588cdf82d
Created April 2, 2015 14:55
Play 2.2.x WSClientVisitor
// define our events
case class RegisterSocket()
case class Connected(out: Enumerator[JsValue], ref: ActorRef)
class WSClientVisitor(name: String) extends Actor with ActorLogging with WebSocketChannel
{
def receive = {
case RegisterSocket => {
// close over sender
val myClient = sender
@dat-vikash
dat-vikash / gist:a2748f0da202387b5b4a
Created April 2, 2015 14:56
Play 2.2.x Controller
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
@dat-vikash
dat-vikash / gist:858207b920ffac3dcb21
Created April 2, 2015 14:58
Play 2.2.x MockWebSocketChannel
trait MockWebSocketChannel extends WebSocketChannel
{
var mockWebSocketChannelQueue : List[JsValue] = List.empty
override def push(data: JsValue): Unit = mockWebSocketChannelQueue = mockWebSocketChannelQueue :+ data
}
@dat-vikash
dat-vikash / gist:2beaf89e541f481f441c
Last active August 29, 2015 14:18
Play 2.2.x WSClientVisitorSpec
class WSClientVisitorSpec extends TestKit(_system = Akka.system(FakeApplication())) with WordSpecLike with Matchers with ImplicitSender
{
//instantiate test constants
val actorRef = TestActorRef(new WSClientVisitor("TEST") with MockWebSocketChannel, name= "test")
// get a test reference to our actor
val actor = actorRef.underlyingActor
"Web Socket Client For Visitor" should {
"register a new socket" in new WithApplication(app = FakeApplication(additionalConfiguration =Map("akka.event-handlers" -> List("akka.testkit.TestEventListener")),
withGlobal = Some(new GlobalSettings() {