Created
July 12, 2015 15:35
-
-
Save omgmog/a1d9992ea487acd34810 to your computer and use it in GitHub Desktop.
socket.io signalling server
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
/*jslint node: true */ | |
(function (){ | |
'use strict'; | |
var nodeStatic = require('node-static'); | |
var http = require('http'); | |
var io = require('socket.io')(); | |
var server = new(nodeStatic.Server)(); | |
var app = http.createServer(function (request, response) { | |
server.serve(request, response); | |
}); | |
app.listen(1337); | |
io.listen(app); | |
// Allows debouncing of functions, to stop event spam | |
function debounce(fn, delay) { | |
var timer = null; | |
return function () { | |
var context = this, args = arguments; | |
clearTimeout(timer); | |
timer = setTimeout(function () { | |
fn.apply(context, args); | |
}, delay); | |
}; | |
} | |
// Since we can't do clients.length in Socket.io 1+, this will give us the count | |
var getClientsForRoom = function (roomId, namespace) { | |
var res = [], | |
ns = io.of(namespace ||'/'); // the default namespace is '/' | |
if (ns) { | |
for (var id in ns.connected) { | |
if(roomId) { | |
var index = ns.connected[id].rooms.indexOf(roomId) ; | |
if(index !== -1) { | |
res.push(ns.connected[id]); | |
} | |
} else { | |
res.push(ns.connected[id]); | |
} | |
} | |
} | |
return res; | |
}; | |
io.sockets.on('connection', function (socket){ | |
var log = function () { | |
var messages = ['>>> Message from server: ']; | |
for (var i = 0; i < arguments.length; i++) { | |
messages.push(arguments[i]); | |
} | |
socket.emit('log', messages); | |
}; | |
socket.on('error', function (err) { | |
console.log(err); | |
}); | |
socket.on('message', function (message) { | |
log('Got message:', message); | |
socket.broadcast.emit('message', message); | |
}); | |
socket.on('create or join', function (room) { | |
var clients = getClientsForRoom(room); | |
var numClients = Object.keys(clients).length; | |
log('Room ' + room + ' has ' + numClients + ' client' + ( numClients === 1 ? '' : 's' )); | |
log('Request to create or join room ' + room); | |
if (numClients === 0){ | |
socket.join(room); | |
socket.emit('created', room); | |
log('Created a room: ', room); | |
} else if (numClients >= 1) { | |
io.sockets.in(room).emit('join', room); | |
socket.join(room); | |
socket.emit('joined', room); | |
log('Joined a room: ', room); | |
} | |
socket.emit('emit(): client ' + socket.id + ' joined room ' + room); | |
socket.broadcast.emit('broadcast(): client ' + socket.id + ' joined room ' + room); | |
}); | |
// Sends slide change event to all clients | |
socket.on('changeSlide', function (slide) { | |
log('Asked to change to slide', slide); | |
socket.broadcast.emit('changeSlide', slide); | |
socket.emit('changeSlide', slide); | |
}); | |
// Sends rotate data to all clients | |
socket.on('rotate', debounce(function (data) { | |
log('Rotation data: ', data); | |
socket.broadcast.emit('rotate', data); | |
socket.emit('rotate', data); | |
}, 50)); | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment