Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- console.log("Starting server...");
- //config and variables
- var serverPort = 8080;
- var connectCounter = 0;
- var adminSessions = [];
- var updater_msDelay = 1000 * 60;
- var updater_dailymotionID = "x1ulpje";
- //libs
- var io = require("socket.io")(serverPort);
- var mysql = require("mysql");
- var http = require("http");
- var https = require("https");
- var memcached = require("memcached");
- var phpunserialize = require("php-unserialize");
- var cookie = require("cookie");
- var util = require('util');
- var sessions = new memcached("localhost:11211");
- sessions.on('failure', function(details) {
- console.log("Error while connecting to PHP sessions manager. error: " + details);
- return -1;
- });
- //events
- var Manager = new ClientEventsManager(io);
- console.log("Server started");
- process.on('exit', function(code) {
- console.log("Server shutting down with code " + code + "...");
- db.destroy();
- sessions.end();
- });
- /******************************************************************************/
- //functions
- function getJSON(options, onResult) {
- var prot = options.port == 443 ? https : http;
- var req = prot.request(options, function(res) {
- var output = '';
- res.setEncoding('utf8');
- res.on('data', function(chunk) {
- output += chunk;
- });
- res.on('end', function() {
- try {
- var obj = JSON.parse(output);
- onResult(res.statusCode, obj);
- } catch (e) {
- console.log("Invalid response returned from "+options.host+options.path+":"+options.port+":\n"+output);
- }
- });
- });
- req.on('error', function(err) {
- console.log("Error in http request to " + options.host + " : " + err);
- });
- req.end();
- }
- function isAuthentified(socket, Yep, Nope) {
- console.log("Trying to retrieve data...");
- socket.send("Your PHP session id: "+Manager.clients[socket.id].PHPSESSID+", right?");
- sessions.get(Manager.clients[socket.id].PHPSESSID, function(err, data) {
- if (err) {
- console.log("err");
- Nope();
- }
- if (data) {
- console.log("data OK");
- var SessionData = PHPunserialize.unserializeSession(data);
- console.log(util.inspect(SessionData, null));
- if (SessionData.authentified) {
- console.log("auth ok");
- Yep();
- } else {
- console.log("no auth");
- Nope();
- }
- } else {
- console.log("data not ok: "+data);
- Nope();
- }
- });
- }
- /******************************************************************************/
- //classes
- function ClientEventsManager() {
- var EventsManager = this;
- this.clients = [];
- this.clientsCount = 0;
- this.onClientConnection = function(socket) {
- var PHPSESSID = cookie.parse(socket.request.headers.cookie).PHPSESSID;
- socket.emit("info", PHPSESSID);
- EventsManager.clientsCount++;
- EventsManager.clients[socket.id] = {};
- EventsManager.clients[socket.id].PHPSESSID = PHPSESSID;
- isAuthentified(socket, function() {
- socket.emit("authentified", true);
- }, function() {
- socket.emit("authentified", false);
- });
- };
- this.onClientDisconnection = function(socket) {
- delete EventsManager.clients[socket.id];
- EventsManager.clientsCount--;
- //remove the client from the sendConnectedClientsOnUpdate array (if he is in)
- if (EventsManager.sendConnectedClientsOnUpdate.indexOf(socket) > -1) {
- EventsManager.sendConnectedClientsOnUpdate.splice(EventsManager.sendConnectedClientsOnUpdate.indexOf(socket), 1);
- }
- };
- this.createEventReceivers = function() {
- io.sockets.on('connection', function(socket) {
- EventsManager.onClientConnection(socket);
- socket.on('disconnect', function() {
- EventsManager.onClientDisconnection(socket);
- });
- });
- };
- this.createEventReceivers();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement