Last active
December 20, 2015 08:59
-
-
Save stannehill/6104937 to your computer and use it in GitHub Desktop.
Backend code for socket.io blog post
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) | |
, io = require('socket.io').listen(app) | |
, fs = require('fs'); | |
app.listen(4000); | |
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); | |
}); | |
} | |
var playernum = 0; | |
io.sockets.on('connection', function (socket) { | |
console.log('connected successfully...'); | |
playernum === 1 ? playernum = 0 : playernum++; | |
socket.emit('player', {'player': playernum}); | |
socket.on('volley', function (input) { | |
socket.broadcast.emit('return', {'data': 1}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment