Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from CodeRarity/master
[examples] socket.io example
- Loading branch information
Showing
3 changed files
with
51 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,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! |
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,8 @@ | ||
<script src="/socket.io/socket.io.js"></script> | ||
<script> | ||
var socket = io.connect('http://localhost'); | ||
socket.on('news', function (data) { | ||
console.log(data); | ||
socket.emit('my other event', { my: 'data' }); | ||
}); | ||
</script> |
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,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); | ||
}); | ||
}); |