-
-
Save sroebuck/1157166 to your computer and use it in GitHub Desktop.
'Simple' example of Atmosphere Scala and Scalatra
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
package com.proinnovate.webfunction | |
import org.scalatra.ScalatraServlet | |
import akka.actor.{Scheduler, Actor} | |
import javax.servlet.http.{HttpServletResponse, HttpServletRequest} | |
import org.atmosphere.cpr.BroadcastFilter.BroadcastAction | |
import org.atmosphere.cpr._ | |
import org.atmosphere.util.XSSHtmlFilter | |
import collection.JavaConversions._ | |
import java.util.concurrent.{LinkedBlockingQueue, TimeUnit} | |
import collection.mutable.{ListBuffer, Queue, LinkedList} | |
import com.weiglewilczek.slf4s.Logging | |
import org.joda.time.DateTime | |
case class AtmosphereCallback(callback: String => Unit) | |
class EventsLogger extends AtmosphereResourceEventListener with Logging { | |
def onThrowable(event: AtmosphereResourceEvent[HttpServletRequest, HttpServletResponse]) { | |
logger.info("onThrowable(): %s".format(event)) | |
} | |
def onBroadcast(event: AtmosphereResourceEvent[HttpServletRequest, HttpServletResponse]) { | |
logger.info("onBroadcast(): %s".format(event)) | |
} | |
def onDisconnect(event: AtmosphereResourceEvent[HttpServletRequest, HttpServletResponse]) { | |
logger.info("onDisconnect(): %s".format(event)) | |
} | |
def onResume(event: AtmosphereResourceEvent[HttpServletRequest, HttpServletResponse]) { | |
logger.info("onResume(): %s".format(event)) | |
} | |
def onSuspend(event: AtmosphereResourceEvent[HttpServletRequest, HttpServletResponse]) { | |
logger.info("onSuspend(): %s".format(event)) | |
} | |
} | |
class AtmosphereActor() extends Actor { | |
protected def receive = { | |
case s: String => { | |
val b = BroadcasterFactory.getDefault.lookup(classOf[DefaultBroadcaster], "myBroadcaster") | |
b.broadcast(s) | |
} | |
case 'init => { | |
val RFC2822_DATE = "EEE, dd MMM yyyy HH:mm:ss Z" | |
Scheduler.schedule(self, "the time: " + DateTime.now.toString(RFC2822_DATE), 500, 500, TimeUnit.MILLISECONDS) | |
} | |
} | |
} | |
class AtmosphereServlet extends ScalatraServlet with Logging { | |
val act = Actor.actorOf(new AtmosphereActor).start() | |
get("/the_stream") { | |
logger.info("Got here!") | |
// Create a Meteor object associated with the HTTP request that can be used to send an immediate or delayed | |
// response... | |
val m: Meteor = Meteor.build(request) | |
// Add a new AtmosphereResourceEventListener to the Meteor. This object will be invoked whenever a connection is | |
// closed or a message broadcast... | |
m.addListener(new EventsLogger()) | |
// Create a Broadcaster - something that appears to do the actual sending of messages by some asynchronous method | |
// or another, and give it the name "myBroadcaster"... | |
val b = new DefaultBroadcaster("myBroadcaster") | |
// Specify that this broadcaster can only be used to respond to the specific request that invoked it... | |
b.setScope(Broadcaster.SCOPE.REQUEST) | |
// Associate a broadcaster with the Meteor so the meteor can now broadcast! | |
m.setBroadcaster(b) | |
// Set off the example actor to send some delayed messages (... this is not Atmosphere specific at all ...)... | |
act ! 'init | |
// Suspend the request response indefinitely ( `-1` means indefinitely, a positive value would be for a time | |
// probably in milliseconds)... | |
m.suspend(-1, false) | |
"" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment