casualjim
12/14/2010 - 3:29 PM

gistfile1.scala

package com.mojolly.websocket

import org.eclipse.jetty.server.handler.AbstractHandler
import org.eclipse.jetty.server.{Request, Server}
import java.io.IOException
import javax.servlet.ServletException
import javax.servlet.http.{HttpServlet, HttpServletResponse, HttpServletRequest}
import org.eclipse.jetty.servlet.{ServletHolder, ServletContextHandler}
import org.eclipse.jetty.server.session.SessionHandler
import akka.actor.Scheduler
import collection.JavaConversions._
import org.eclipse.jetty.continuation.{ContinuationSupport, Continuation}
import java.util.concurrent.{LinkedBlockingQueue, TimeUnit, CopyOnWriteArrayList}
import org.eclipse.jetty.websocket.{WebSocketServlet, WebSocket}
import java.lang.String
import org.eclipse.jetty.websocket.WebSocket.Outbound
import org.eclipse.jetty.webapp.WebAppContext

class HelloWorldServlet extends HttpServlet {

  override protected def doGet(req: HttpServletRequest, resp: HttpServletResponse) {
    resp.setContentType("text/html")
    resp.setStatus(200)
    resp.getWriter.println("<h1>Hello World!!!</h1>")
    resp.getWriter.println("session=" + req.getSession(true).getId)
  }
}

trait Connection {
  def write(msg: String)
}

class ContinuationConnection(cont: Continuation) extends Connection {
  def write(msg: String) {

  }
}

object Connection {
  val connections = new LinkedBlockingQueue[Continuation]
  val webSockets = new CopyOnWriteArrayList[TestWebSocket]
  var i = 0
  Scheduler.schedule(() => {
  println("There are %s connections" format (connections.size + webSockets.size))
  i += 1
  val cont = connections.peek
  if(cont != null) {
    println("resuming")
    cont.resume
    cont.getServletResponse.getWriter.println("hello again " + i)
    cont.getServletResponse.flushBuffer
  }
  webSockets foreach { ws =>
    ws.outbound foreach { _ sendMessage "hello from websocket" }
  }

  }, 2, 1, TimeUnit.SECONDS)
}

import Connection._

class CometServlet extends HttpServlet {

  override protected def doGet(req: HttpServletRequest, resp: HttpServletResponse) {
    println("Got request for continuation")
    val cont = ContinuationSupport.getContinuation(req)
    println(cont.toString)
    if (cont.isInitial) {
      connections.add(cont)
      cont.getServletResponse.getWriter.println("hello...")
      cont.getServletResponse.flushBuffer
    }

    cont.suspend()
  }

}

class TestWebSocket extends WebSocket {

  private var _outbound: Outbound = null
  def outbound = {
    Option(_outbound)
  }

  def onDisconnect = {}

  def onMessage(p1: Byte, p2: Array[Byte], p3: Int, p4: Int) = {

  }

  def onFragment(p1: Boolean, p2: Byte, p3: Array[Byte], p4: Int, p5: Int) = {

  }

  def onMessage(p1: Byte, p2: String) = {
    println("Got message:\n" + p2)
  }

  def onConnect(p1: Outbound) = {
    _outbound = p1
    webSockets add this
  }
}

class WebSocketActorServlet extends WebSocketServlet {
  override def doGet(req: HttpServletRequest, resp: HttpServletResponse) {
    getServletContext().getNamedDispatcher("default").forward(req, resp)
  }

  def doWebSocketConnect(req: HttpServletRequest, protocol: String) = {
    new TestWebSocket
  }

}
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
  }
}