Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 KB | None | 0 0
  1. require('heapdump');
  2.  
  3. console.log(process.pid);
  4. setInterval(function () {
  5. global.gc();
  6. var heapUsed = process.memoryUsage().heapUsed;
  7. console.log("Program is using " + heapUsed + " bytes of Heap.")
  8. // process.kill(process.pid, 'SIGUSR2');
  9. }, 1000);
  10.  
  11.  
  12. var Timers = require('./lib/timers.js');
  13. var config = require('./lib/config.js')
  14. var t = new Timers;
  15.  
  16. var app = require('http').createServer(handler),
  17. io = require('socket.io').listen(app),
  18. fs = require('fs'),
  19. mysql = require('mysql'),
  20. connectionsArray = [],
  21. connection = mysql.createConnection({
  22. host: 'localhost',
  23. user: config.mysql.username,
  24. password: config.mysql.password,
  25. database: config.mysql.database,
  26. }),
  27. POLLING_INTERVAL = 1000,
  28. pollingTimer, pollingTimer2, pollingTimer3;
  29.  
  30. // If there is an error connecting to the database
  31. connection.connect(function(err) {
  32. // connected! (unless `err` is set)
  33. if (err) {
  34. console.log(err);
  35. }
  36. });
  37.  
  38. // creating the server ( localhost:8000 )
  39. app.listen(8000);
  40.  
  41. // on server started we can load our client.html page
  42. function handler(req, res) {
  43. fs.readFile(__dirname + '/index.php', function(err, data) {
  44. if (err) {
  45. console.log(err);
  46. res.writeHead(500);
  47. return res.end('Error loading index.php');
  48. }
  49. res.writeHead(200);
  50. res.end(data);
  51.  
  52. });
  53. }
  54.  
  55.  
  56.  
  57. var pollingLoop = function() {
  58.  
  59. // Doing the database query
  60. var time=Math.round(new Date().getTime()/1000 - 86400);
  61. 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";
  62.  
  63. var query = connection.query(sql),
  64. users = []; // this array will contain the result of our db query
  65.  
  66. // setting the query listeners
  67. query
  68. .on('error', function(err) {
  69. // Handle error, and 'end' event will be emitted after this as well
  70. console.log(err);
  71. updateSockets(err);
  72. })
  73. .on('result', function(user) {
  74. // it fills our array looping on each user row inside the db
  75. users.push(user);
  76. })
  77. .on('end', function() {
  78. // loop on itself only if there are sockets still connected
  79. if (connectionsArray.length) {
  80.  
  81. pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL);
  82.  
  83. updateSockets({
  84. users: users
  85. });
  86. } else {
  87.  
  88. console.log('The server timer was stopped because there are no more socket connections on the app')
  89.  
  90. }
  91. });
  92.  
  93.  
  94. };
  95.  
  96.  
  97. var pollingLoop2 = function() {
  98.  
  99.  
  100. // Doing the database query
  101. var time=Math.round(new Date().getTime()/1000 - 86400);
  102. 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";
  103.  
  104. var query_this = connection.query(sql_this),
  105. users_this = []; // this array will contain the result of our db query
  106.  
  107. // setting the query listeners
  108. query_this
  109. .on('error', function(err) {
  110. // Handle error, and 'end' event will be emitted after this as well
  111. console.log(err);
  112. updateSockets(err);
  113. })
  114. .on('result', function(user) {
  115. // it fills our array looping on each user row inside the db
  116.  
  117. users_this.push(user);
  118. })
  119. .on('end', function() {
  120. // loop on itself only if there are sockets still connected
  121. if (connectionsArray.length) {
  122.  
  123. pollingTimer2 = setTimeout(pollingLoop2, POLLING_INTERVAL);
  124.  
  125. updateSockets2({
  126. users_this: users_this,
  127. time:time
  128. });
  129. } else {
  130.  
  131. console.log('The server timer was stopped because there are no more socket connections on the app')
  132.  
  133. }
  134. });
  135.  
  136. ///
  137. };
  138.  
  139.  
  140. var pollingLoop3 = function() {
  141.  
  142.  
  143. // Doing the database query
  144. var time=Math.round(new Date().getTime()/1000 - 86400);
  145. var sql_this="SELECT * FROM time_option ORDER BY minutes";
  146.  
  147. var query_this = connection.query(sql_this),
  148. option = []; // this array will contain the result of our db query
  149.  
  150. // setting the query listeners
  151. query_this
  152. .on('error', function(err) {
  153. // Handle error, and 'end' event will be emitted after this as well
  154. console.log(err);
  155. updateSockets(err);
  156. })
  157. .on('result', function(user) {
  158. // it fills our array looping on each user row inside the db
  159. option.push(user);
  160. })
  161. .on('end', function() {
  162. // loop on itself only if there are sockets still connected
  163. if (connectionsArray.length) {
  164.  
  165. pollingTimer3 = setTimeout(pollingLoop3, POLLING_INTERVAL);
  166.  
  167. updateSockets3({
  168. option: option
  169. });
  170. } else {
  171.  
  172. console.log('The server timer was stopped because there are no more socket connections on the app')
  173.  
  174. }
  175. });
  176.  
  177. ///
  178. };
  179.  
  180.  
  181. var pollingLoop4 = function() {
  182.  
  183.  
  184. // Doing the database query
  185. var time=Math.round(new Date().getTime()/1000 - 86400);
  186. var sql_this="SELECT * FROM rates order by id desc limit 1";
  187.  
  188. var query_this = connection.query(sql_this),
  189. rate = []; // this array will contain the result of our db query
  190.  
  191. // setting the query listeners
  192. query_this
  193. .on('error', function(err) {
  194. // Handle error, and 'end' event will be emitted after this as well
  195. console.log(err);
  196. updateSockets(err);
  197. })
  198. .on('result', function(user) {
  199. // it fills our array looping on each user row inside the db
  200. rate.push(user);
  201. })
  202. .on('end', function() {
  203. // loop on itself only if there are sockets still connected
  204. if (connectionsArray.length) {
  205.  
  206. pollingTimer4 = setTimeout(pollingLoop4, 2000);
  207.  
  208. updateSockets4({
  209. rate: rate,
  210. time: (new Date()).getTime()
  211. });
  212. } else {
  213.  
  214. console.log('The server timer was stopped because there are no more socket connections on the app')
  215.  
  216. }
  217. });
  218.  
  219. ///
  220. };
  221.  
  222.  
  223. // creating a new websocket to keep the content updated without any AJAX request
  224. io.sockets.on('connection', function(socket) {
  225.  
  226. // console.log('Number of connections:' + connectionsArray.length);
  227. // starting the loop only if at least there is one user connected
  228. if (!connectionsArray.length) {
  229. pollingLoop();
  230. pollingLoop2();
  231. pollingLoop3();
  232. pollingLoop4();
  233. }
  234.  
  235. socket.on('disconnect', function() {
  236. var socketIndex = connectionsArray.indexOf(socket);
  237. // console.log('socketID = %s got disconnected', socketIndex);
  238. if (~socketIndex) {
  239. connectionsArray.splice(socketIndex, 1);
  240. }
  241. });
  242.  
  243. //console.log('A new socket is connected!');
  244.  
  245. connectionsArray.push(socket);
  246. // console.log(connectionsArray.length);
  247. });
  248.  
  249. var updateSockets = function(data) {
  250. // adding the time of the last update
  251. data.time = new Date();
  252. //console.log('Pushing new data to the clients connected ( connections amount = %s ) - %s', connectionsArray.length , data.time);
  253. // sending new data to all the sockets connected
  254. connectionsArray.forEach(function(tmpSocket) {
  255. tmpSocket.volatile.emit('notification', data);
  256. });
  257. };
  258.  
  259. var updateSockets2= function(data) {
  260. // adding the time of the last update
  261. data.time = new Date();
  262. // console.log('Pushing new data to the clients connected ( connections amount = %s ) - %s', connectionsArray.length , data.time);
  263. // sending new data to all the sockets connected
  264. connectionsArray.forEach(function(tmpSocket) {
  265. tmpSocket.volatile.emit('notification2', data);
  266. });
  267. };
  268. var updateSockets3= function(data) {
  269. // adding the time of the last update
  270. data.time = new Date();
  271. //console.log('Pushing new data to the clients connected ( connections amount = %s ) - %s', connectionsArray.length , data.time);
  272. // sending new data to all the sockets connected
  273. connectionsArray.forEach(function(tmpSocket) {
  274. tmpSocket.volatile.emit('notification3', data);
  275. });
  276. };
  277.  
  278. var updateSockets4= function(data) {
  279. // adding the time of the last update
  280. data.time = new Date();
  281. // console.log('Pushing new data to the clients connected ( connections amount = %s ) - %s', connectionsArray.length , data.time);
  282. // sending new data to all the sockets connected
  283. connectionsArray.forEach(function(tmpSocket) {
  284. tmpSocket.volatile.emit('notification4', data);
  285. });
  286. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement