slush-y
Version:
A slush generator for Best Practices with AngularJS Fullstack applications.
67 lines (56 loc) • 2.1 kB
JavaScript
/**
* Socket.io configuration
*/
;
var config = require('./environment');
var indexer = require('../components/indexer');
var path = require('path');
// When the user disconnects.. perform this
function onDisconnect(socket) {
}
// When the user connects.. perform this
function onConnect(socket) {
// When the client emits 'info', this listens and executes
socket.on('info', function (data) {
console.info('[%s] %s', socket.address, JSON.stringify(data, null, 2));
});
var routes = indexer.all('server/api')
routes.forEach(function (item){
require( path.join(item.path, item.name+'.socket') ).register(socket);
})
// globber('./server/api/**/*.socket.js').forEach(function( seedPath ) {
// require(path.resolve( seedPath )).register(socket);
// });
// Insert sockets below
// require('../api/thing/thing.socket').register(socket);
// require('../api/generator/generator.socket').register(socket);
}
module.exports = function (socketio) {
// socket.io (v1.x.x) is powered by debug.
// In order to see all the debug output, set DEBUG (in server/config/local.env.js) to including the desired scope.
//
// ex: DEBUG: "http*,socket.io:socket"
// We can authenticate socket.io users and access their token through socket.handshake.decoded_token
//
// 1. You will need to send the token in `client/components/socket/socket.service.js`
//
// 2. Require authentication here:
// socketio.use(require('socketio-jwt').authorize({
// secret: config.secrets.session,
// handshake: true
// }));
socketio.on('connection', function (socket) {
socket.address = socket.handshake.address !== null ?
socket.handshake.address.address + ':' + socket.handshake.address.port :
process.env.DOMAIN;
socket.connectedAt = new Date();
// Call onDisconnect.
socket.on('disconnect', function () {
onDisconnect(socket);
console.info('[%s] DISCONNECTED', socket.address);
});
// Call onConnect.
onConnect(socket);
console.info('[%s] CONNECTED', socket.address);
});
};