ensl_gathers/config/socketio.js

40 lines
1 KiB
JavaScript
Raw Normal View History

2015-07-20 09:41:58 +00:00
"use strict";
2015-07-22 15:35:40 +00:00
var winston = require("winston");
2015-07-22 23:30:14 +00:00
var User = require("../lib/user/user");
2015-07-27 11:55:36 +00:00
var client = require("../lib/ensl/client")();
2015-07-22 23:30:14 +00:00
var chatController = require("../lib/chat/controller");
var gatherController = require("../lib/gather/controller");
var userController = require("../lib/user/controller");
2015-07-21 14:10:24 +00:00
2015-07-27 11:55:36 +00:00
var getRandomUser = function (callback) {
var id = Math.floor(Math.random() * 5000) + 1;
client.getUserById({
id: id
}, function (error, response, body) {
if (response.statusCode !== 200) return getRandomUser(callback);
return callback(error, response, body);
});
};
2015-07-26 11:54:35 +00:00
2015-07-20 09:41:58 +00:00
module.exports = function (io) {
var rootNamespace = io.of('/')
2015-07-21 00:24:14 +00:00
// Authorisation
io.use(function (socket, next) {
2015-07-27 11:55:36 +00:00
getRandomUser(function (error, _, body) {
2015-07-22 15:35:40 +00:00
if (error) {
winston.error(error);
return next(error)
};
2015-07-22 23:30:14 +00:00
socket._user = new User(body);
2015-07-28 23:32:50 +00:00
console.log("You:", body.username, body.id);
2015-07-22 00:00:05 +00:00
next();
2015-07-27 11:55:36 +00:00
})
2015-07-21 00:24:14 +00:00
});
userController(rootNamespace);
chatController(rootNamespace);
gatherController(rootNamespace);
};