Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Baulig committed Mar 22, 2012
0 parents commit f9607df
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Makefile
@@ -0,0 +1,7 @@
APP=./app.js
NODE=node
.PHONY: run

run:
$(NODE) $(APP)

13 changes: 13 additions & 0 deletions README.md
@@ -0,0 +1,13 @@
# _socket.io and Express. Tying it all together. Demo Application_

This is a demo application demonstrating how the two popular frameworks socket.io and Express can be effectively used together.

Please read my article [socket.io and Express. Tying it all together._](http://www.danielbaulig.de/spcket-ioexpress) for details on what is going on.

# Installation

npm install sioe-demo

# Run

make run
55 changes: 55 additions & 0 deletions app.js
@@ -0,0 +1,55 @@
/*jshint laxcomma:true*/

var io = require('socket.io')
, express = require('express')
, util = require('util')
, app = express.createServer()
, connect = require('express/node_modules/connect')
, parseCookie = connect.utils.parseCookie
, MemoryStore = connect.middleware.session.MemoryStore
, store;

app.configure(function () {
app.set('view engine', 'jade');
app.set('view options', {layout: false});
app.use(express.cookieParser());
app.use(express.session({
secret: 'secret'
, key: 'express.sid'
, store: store = new MemoryStore()
}));
});

app.get('/', function (req, res) {
res.render('index', {value: req.session.value});
});

app.listen(8080);

io.listen(app).set('authorization', function (data, accept) {
if (!data.headers.cookie)
return accept('No cookie transmitted.', false);

data.cookie = parseCookie(data.headers.cookie);
data.sessionID = data.cookie['express.sid'];

store.load(data.sessionID, function (err, session) {
if (err || !session) return accept('Error', false);

data.session = session;
return accept(null, true);
});
}).sockets.on('connection', function (socket) {
var sess = socket.handshake.session;
socket.log.info(
'a socket with sessionID'
, socket.handshake.sessionID
, 'connected'
);
socket.on('set value', function (val) {
sess.reload(function () {
sess.value = val;
sess.touch().save();
});
});
});
22 changes: 22 additions & 0 deletions package.json
@@ -0,0 +1,22 @@
{
"author": "Daniel Baulig <daniel.baulig@gmx.de> (http://www.danielbaulig.de)",
"name": "sioe-demo",
"description": "socket.io and Express. Tying it all together. Demo Application",
"version": "0.0.1",
"homepage": "http://www.danielbaulig.de/socket-ioexpress/",
"repository": {
"type": "git",
"url": "git://github.com/DanielBaulig/sioe-demo.git"
},
"main": "app.js",
"dependencies": {
"socket.io": "*"
, "express": "*"
, "jade": "*"
},
"devDependencies": {},
"optionalDependencies": {},
"engines": {
"node": "*"
}
}
Binary file added views/.index.jade.swp
Binary file not shown.
24 changes: 24 additions & 0 deletions views/index.jade
@@ -0,0 +1,24 @@
!!! html
html
head
title Tying it all together demo
script(src='/socket.io/socket.io.js')
script(src='http://code.jquery.com/jquery-latest.js')
body
if value
h2 your session value is #{value}
else
h2 set your session value!
input#input
script
$(function () {
var socket = io.connect()
, input = $('#input');
input.keypress(function (e) {
if (e.which === 13) {
socket.emit('set value', input.val());
input.val('');
$('h2').text('value set, reload page');
}
}).focus();
});

0 comments on commit f9607df

Please sign in to comment.