forked from DanielBaulig/sioe-demo
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniel Baulig
committed
Mar 22, 2012
0 parents
commit f9607df
Showing
6 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
APP=./app.js | ||
NODE=node | ||
.PHONY: run | ||
|
||
run: | ||
$(NODE) $(APP) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); |