Created
March 30, 2015 20:54
-
-
Save JulienBreux/24b8287801c9c9d0b1c6 to your computer and use it in GitHub Desktop.
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
var ioClient = require('socket.io-client')('http://127.0.0.1'); | |
ioClient.on('connect', function () { | |
ioClient.emit('broadcast', 'first-event', 'Hello'); | |
ioClient.emit('broadcast', 'second-event', 'World'); | |
}); |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
var socket = io('http://127.0.0.1'); | |
socket.on('first-event', function (data) { | |
console.log(data); | |
}); | |
socket.on('second-event', function (data) { | |
console.log(data); | |
}); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
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
var app = require('http').createServer(handler); | |
var io = require('socket.io')(app); | |
var fs = require('fs'); | |
app.listen(80); | |
function handler (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); | |
}); | |
} | |
io.on('connection', function (socket) { | |
socket.on('broadcast', function () { | |
socket.broadcast.emit.apply(socket.broadcast, arguments); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment