From 34bf456901634f35b1b1c3f5763ba07dbd8492cd Mon Sep 17 00:00:00 2001 From: Christian Howe Date: Sun, 27 Nov 2011 19:05:53 -0500 Subject: [PATCH] Added an example of using Socket.io with Union. --- examples/socketio/README | 13 +++++++++++++ examples/socketio/index.html | 8 ++++++++ examples/socketio/server.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 examples/socketio/README create mode 100644 examples/socketio/index.html create mode 100644 examples/socketio/server.js diff --git a/examples/socketio/README b/examples/socketio/README new file mode 100644 index 0000000..9788811 --- /dev/null +++ b/examples/socketio/README @@ -0,0 +1,13 @@ +This folder contains an example of how to use Union with Socket.io. + +First, you'll want to install both Union and Socket.io. Run this +command in the folder you placed these two files: + +npm install union socket.io + +You can run the server like so: + +node server.js + +Now open up your web browser to http://localhost and see the results +in the console! diff --git a/examples/socketio/index.html b/examples/socketio/index.html new file mode 100644 index 0000000..fd8dc8c --- /dev/null +++ b/examples/socketio/index.html @@ -0,0 +1,8 @@ + + \ No newline at end of file diff --git a/examples/socketio/server.js b/examples/socketio/server.js new file mode 100644 index 0000000..3e7bc27 --- /dev/null +++ b/examples/socketio/server.js @@ -0,0 +1,30 @@ +var fs = require('fs'), + union = require('union'); + +var server = union.createServer({ + before: [ + function (req, res) { + fs.readFile(__dirname + '/index.html', + function (err, data) { + if (err) { + res.writeHead(500); + return res.end('Error loading index.html'); + } + + res.writeHead(200); + res.end(data); + }); + } + ] +}); + +server.listen(8080); + +var io = require('socket.io').listen(server); + +io.sockets.on('connection', function(socket) { + socket.emit('news', {hello: 'world'}); + socket.on('my other event', function(data) { + console.log(data); + }); +}); \ No newline at end of file