Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require('heapdump');
- console.log(process.pid);
- setInterval(function () {
- global.gc();
- var heapUsed = process.memoryUsage().heapUsed;
- console.log("Program is using " + heapUsed + " bytes of Heap.")
- // process.kill(process.pid, 'SIGUSR2');
- }, 1000);
- var Timers = require('./lib/timers.js');
- var config = require('./lib/config.js')
- var t = new Timers;
- var app = require('http').createServer(handler),
- io = require('socket.io').listen(app),
- fs = require('fs'),
- mysql = require('mysql'),
- connectionsArray = [],
- connection = mysql.createConnection({
- host: 'localhost',
- user: config.mysql.username,
- password: config.mysql.password,
- database: config.mysql.database,
- }),
- POLLING_INTERVAL = 1000,
- pollingTimer, pollingTimer2, pollingTimer3;
- // If there is an error connecting to the database
- connection.connect(function(err) {
- // connected! (unless `err` is set)
- if (err) {
- console.log(err);
- }
- });
- // creating the server ( localhost:8000 )
- app.listen(8000);
- // on server started we can load our client.html page
- function handler(req, res) {
- fs.readFile(__dirname + '/index.php', function(err, data) {
- if (err) {
- console.log(err);
- res.writeHead(500);
- return res.end('Error loading index.php');
- }
- res.writeHead(200);
- res.end(data);
- });
- }
- var pollingLoop = function() {
- // Doing the database query
- var time=Math.round(new Date().getTime()/1000 - 86400);
- var sql="SELECT o.*, top.minutes FROM options o inner join time_option top on top.id=o.id_time where o.finished!=0 and o.end_timestamp >= "+time+" order by o.id desc";
- var query = connection.query(sql),
- users = []; // this array will contain the result of our db query
- // setting the query listeners
- query
- .on('error', function(err) {
- // Handle error, and 'end' event will be emitted after this as well
- console.log(err);
- updateSockets(err);
- })
- .on('result', function(user) {
- // it fills our array looping on each user row inside the db
- users.push(user);
- })
- .on('end', function() {
- // loop on itself only if there are sockets still connected
- if (connectionsArray.length) {
- pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL);
- updateSockets({
- users: users
- });
- } else {
- console.log('The server timer was stopped because there are no more socket connections on the app')
- }
- });
- };
- var pollingLoop2 = function() {
- // Doing the database query
- var time=Math.round(new Date().getTime()/1000 - 86400);
- var sql_this="SELECT o.*, top.minutes FROM options o inner join time_option top on top.id=o.id_time where o.finished=0 order by o.id desc";
- var query_this = connection.query(sql_this),
- users_this = []; // this array will contain the result of our db query
- // setting the query listeners
- query_this
- .on('error', function(err) {
- // Handle error, and 'end' event will be emitted after this as well
- console.log(err);
- updateSockets(err);
- })
- .on('result', function(user) {
- // it fills our array looping on each user row inside the db
- users_this.push(user);
- })
- .on('end', function() {
- // loop on itself only if there are sockets still connected
- if (connectionsArray.length) {
- pollingTimer2 = setTimeout(pollingLoop2, POLLING_INTERVAL);
- updateSockets2({
- users_this: users_this,
- time:time
- });
- } else {
- console.log('The server timer was stopped because there are no more socket connections on the app')
- }
- });
- ///
- };
- var pollingLoop3 = function() {
- // Doing the database query
- var time=Math.round(new Date().getTime()/1000 - 86400);
- var sql_this="SELECT * FROM time_option ORDER BY minutes";
- var query_this = connection.query(sql_this),
- option = []; // this array will contain the result of our db query
- // setting the query listeners
- query_this
- .on('error', function(err) {
- // Handle error, and 'end' event will be emitted after this as well
- console.log(err);
- updateSockets(err);
- })
- .on('result', function(user) {
- // it fills our array looping on each user row inside the db
- option.push(user);
- })
- .on('end', function() {
- // loop on itself only if there are sockets still connected
- if (connectionsArray.length) {
- pollingTimer3 = setTimeout(pollingLoop3, POLLING_INTERVAL);
- updateSockets3({
- option: option
- });
- } else {
- console.log('The server timer was stopped because there are no more socket connections on the app')
- }
- });
- ///
- };
- var pollingLoop4 = function() {
- // Doing the database query
- var time=Math.round(new Date().getTime()/1000 - 86400);
- var sql_this="SELECT * FROM rates order by id desc limit 1";
- var query_this = connection.query(sql_this),
- rate = []; // this array will contain the result of our db query
- // setting the query listeners
- query_this
- .on('error', function(err) {
- // Handle error, and 'end' event will be emitted after this as well
- console.log(err);
- updateSockets(err);
- })
- .on('result', function(user) {
- // it fills our array looping on each user row inside the db
- rate.push(user);
- })
- .on('end', function() {
- // loop on itself only if there are sockets still connected
- if (connectionsArray.length) {
- pollingTimer4 = setTimeout(pollingLoop4, 2000);
- updateSockets4({
- rate: rate,
- time: (new Date()).getTime()
- });
- } else {
- console.log('The server timer was stopped because there are no more socket connections on the app')
- }
- });
- ///
- };
- // creating a new websocket to keep the content updated without any AJAX request
- io.sockets.on('connection', function(socket) {
- // console.log('Number of connections:' + connectionsArray.length);
- // starting the loop only if at least there is one user connected
- if (!connectionsArray.length) {
- pollingLoop();
- pollingLoop2();
- pollingLoop3();
- pollingLoop4();
- }
- socket.on('disconnect', function() {
- var socketIndex = connectionsArray.indexOf(socket);
- // console.log('socketID = %s got disconnected', socketIndex);
- if (~socketIndex) {
- connectionsArray.splice(socketIndex, 1);
- }
- });
- //console.log('A new socket is connected!');
- connectionsArray.push(socket);
- // console.log(connectionsArray.length);
- });
- var updateSockets = function(data) {
- // adding the time of the last update
- data.time = new Date();
- //console.log('Pushing new data to the clients connected ( connections amount = %s ) - %s', connectionsArray.length , data.time);
- // sending new data to all the sockets connected
- connectionsArray.forEach(function(tmpSocket) {
- tmpSocket.volatile.emit('notification', data);
- });
- };
- var updateSockets2= function(data) {
- // adding the time of the last update
- data.time = new Date();
- // console.log('Pushing new data to the clients connected ( connections amount = %s ) - %s', connectionsArray.length , data.time);
- // sending new data to all the sockets connected
- connectionsArray.forEach(function(tmpSocket) {
- tmpSocket.volatile.emit('notification2', data);
- });
- };
- var updateSockets3= function(data) {
- // adding the time of the last update
- data.time = new Date();
- //console.log('Pushing new data to the clients connected ( connections amount = %s ) - %s', connectionsArray.length , data.time);
- // sending new data to all the sockets connected
- connectionsArray.forEach(function(tmpSocket) {
- tmpSocket.volatile.emit('notification3', data);
- });
- };
- var updateSockets4= function(data) {
- // adding the time of the last update
- data.time = new Date();
- // console.log('Pushing new data to the clients connected ( connections amount = %s ) - %s', connectionsArray.length , data.time);
- // sending new data to all the sockets connected
- connectionsArray.forEach(function(tmpSocket) {
- tmpSocket.volatile.emit('notification4', data);
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement