Skip to content

Instantly share code, notes, and snippets.

@softprops
Created Dec 3, 2011
Embed
What would you like to do?
unfiltered sse
organization := "me.lessis"
name := "sse"
version := "0.1.0-SNAPSHOT"
libraryDependencies ++= Seq(
"net.databinder" %% "unfiltered-netty-server" % "0.5.3-SNAPSHOT",
"net.databinder" %% "unfiltered-event-source" % "0.5.3-SNAPSHOT"
)
<DOCTYPE! html>
<html>
<head>
<script type="text/javascript">
if (!!window.EventSource) {
var source = new EventSource("/servertime");
source.addEventListener('message', function(e) {
console.log(e.data);
}, false);
source.addEventListener('open', function(e) {
console.log("open")
}, false);
source.addEventListener('error', function(e) {
if (e.readyState == EventSource.CLOSED) {
console.log("error")
}
}, false);
} else {
// no support here
}
</script>
</head>
<body></body>
</html>
object Server {
import unfiltered.request._
import unfiltered.response._
import unfiltered.netty
import unfiltered.eventsource._
import unfiltered.eventsource.response._
import unfiltered.eventsource.request._
def main(a: Array[String]) {
netty.Http(8080).resources(getClass().getResource("/www/")).handler(
netty.async.Planify {
case r & Path("/servertime") & EventStream(_) =>
val now = System.currentTimeMillis
r.respond(Events(Message(now.toString, id = Some(now)):: Nil))
}).run
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment