Permalink
Browse files

add sse example

  • Loading branch information...
1 parent 885b546 commit 5f784cefc232d3ab3c9d7ff6936a3125ecef4b2d @hax committed Jun 16, 2012
View
@@ -0,0 +1,38 @@
+
+/**
+ * Module dependencies.
+ */
+
+var express = require('express')
+ , routes = require('./routes');
+
+var app = module.exports = express.createServer();
+
+// Configuration
+
+app.configure(function(){
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'jade');
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(app.router);
+ app.use(express.static(__dirname + '/public'));
+});
+
+app.configure('development', function(){
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+});
+
+app.configure('production', function(){
+ app.use(express.errorHandler());
+});
+
+// Routes
+
+app.get('/', routes.index);
+app.get('/chat', routes.chat);
+app.post('/say', routes.say);
+
+app.listen(3000, function(){
+ console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
+});
View
@@ -0,0 +1,9 @@
+{
+ "name": "application-name"
+ , "version": "0.0.1"
+ , "private": true
+ , "dependencies": {
+ "express": "2.5.8"
+ , "jade": ">= 0.0.1"
+ }
+}
@@ -0,0 +1,8 @@
+body {
+ padding: 50px;
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
+}
+
+a {
+ color: #00B7FF;
+}
@@ -0,0 +1,27 @@
+<!doctype html>
+<form method="post" action="/say" onsubmit="return say(this)">
+ <input type="text">
+ <input type="submit">
+</form>
+
+
+
+<script>
+var es = new EventSource('/chat')
+es.onmessage = function(evt) {
+ if (evt.data !== 'noop')
+ console.log(evt.data)
+}
+
+function say(form) {
+ var words = form.elements[0].value
+ var req = new XMLHttpRequest()
+ req.open(form.method, form.action, false)
+ req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
+ req.send('words=' + words)
+ req.onload = function() {
+ console.log(req.responseText)
+ }
+ return false
+}
+</script>
@@ -0,0 +1,27 @@
+
+/*
+ * GET home page.
+ */
+
+exports.index = function(req, res){
+ res.render('index', { title: 'Express' })
+};
+
+var records = []
+
+exports.chat = function(req, res) {
+ res.writeHead(200, {'Content-Type': 'text/event-stream'});
+ var last = 0
+ setInterval(function() {
+ res.write('data: noop\n\n')
+ for (var i = last; i < records.length; i++)
+ res.write('data: ' + records[i].words + '\n\n')
+ last = i
+ }, 100)
+}
+
+exports.say = function(req, res) {
+ records.push({words:req.body.words, time:Date.now()})
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ res.end('ok')
+}
View
@@ -0,0 +1,21 @@
+var http = require('http');
+http.createServer(function (req, res) {
+ if (req.url == '/client') {
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ res.end("\
+<!doctype html>\
+<script>\
+var es = new EventSource('http://localhost/');\
+es.onmessage = function(evt) {\
+ console.log(evt.data)\
+}\
+</script>\
+ ")
+ } else {
+ res.writeHead(200, {'Content-Type': 'text/event-stream'});
+ setInterval(function() {
+ res.write('data: ' + Date.now() + '\n\n')
+ }, 1000)
+ }
+ //res.end('data: Hello World\n\n');
+}).listen(80, '127.0.0.1');
@@ -0,0 +1,2 @@
+h1= title
+p Welcome to #{title}
@@ -0,0 +1,6 @@
+!!!
+html
+ head
+ title= title
+ link(rel='stylesheet', href='/stylesheets/style.css')
+ body!= body

0 comments on commit 5f784ce

Please sign in to comment.