Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mojolly.websocket
- import org.eclipse.jetty.server.Server
- import javax.servlet.http.{HttpServlet, HttpServletResponse, HttpServletRequest}
- import org.eclipse.jetty.continuation.ContinuationSupport
- import java.lang.String
- import org.eclipse.jetty.webapp.WebAppContext
- import akka.actor.ActorRegistry
- class CometServlet extends HttpServlet with Logging {
- override protected def doGet(req: HttpServletRequest, resp: HttpServletResponse) {
- println("Got request for continuation")
- val cont = ContinuationSupport.getContinuation(req)
- cont.setTimeout(5000)
- println(cont.toString)
- if (cont.isInitial) {
- cont.getServletResponse.getWriter.println("hello...")
- cont.getServletResponse.flushBuffer
- ActorRegistry.actorsFor(classOf[PollerActor]) foreach { _ ! RegisterCallback(
- msg => {
- cont.setAttribute("messages", msg)
- cont.resume
- }
- )}
- }
- if(cont.isResumed && cont.getAttribute("messages") != null) {
- cont.getServletResponse.getWriter.println(cont.getAttribute("messages").toString)
- cont.getServletResponse.flushBuffer
- cont.setAttribute("messages", null)
- }
- if(cont.isExpired) {
- try {
- cont.getServletResponse.getWriter.println("")
- cont.getServletResponse.flushBuffer
- } catch {
- case e => {
- log.warn(e, "A problem writing to the stream on timeout")
- cont.suspend
- cont.complete
- }
- }
- }
- cont.suspend()
- }
- }
- object WebServer {
- def main(args: Array[String]) {
- val server = new Server(8888)
- val context = new WebAppContext
- context.setDescriptor("src/main/webapp/WEB-INF/web.xml")
- context.setResourceBase("src/main/webapp")
- context.setContextPath("/")
- context.setParentLoaderPriority(true)
- server.setHandler(context)
- server.start
- server.join
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement