Last active
February 1, 2016 03:30
-
-
Save hussachai/ebacfb5276b414336ab5 to your computer and use it in GitHub Desktop.
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
class Application extends Controller { | |
... | |
implicit val akkaSystem = akka.actor.ActorSystem() | |
val redis = RedisClient() | |
def index = Action { implicit request => | |
Ok(views.html.pubsub()) | |
} | |
def subscribe = WebSocket.tryAcceptWithActor[String, String] { request => | |
def props(channel: String)(out: ActorRef) = Props(classOf[SubscribeActor], redis, | |
out, Seq(channel), Nil).withDispatcher("rediscala-worker-dispatcher") | |
Future.successful(request.getQueryString("channel") match { | |
case None => Left(Forbidden) | |
case Some(channel) => Right(props(channel)) | |
}) | |
} | |
def publish(channel: String) = Action.async { implicit request => | |
request.body.asFormUrlEncoded.flatMap{ params => | |
params.get("message").map{ message => | |
redis.publish(channel, message.head).map{ n => | |
if(n > 0) { | |
log.debug(s"Number of subscriber: $n s") | |
Ok(n.toString) | |
}else { | |
log.debug("No recipient") | |
Gone | |
} | |
} | |
} | |
}.getOrElse(Future.successful(BadRequest("No content received"))) | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment