Advertisement
Guest User

Untitled

a guest
Aug 1st, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. 'use strict;'
  2.  
  3. var io = require('socket.io');
  4. var fs = require('fs');
  5. var mysql = require('mysql');
  6. var http = require('http');
  7. var config = require("./config");
  8.  
  9. function Server() {
  10. var self = this;
  11. this.db = mysql.createConnection({
  12. host: 'localhost',
  13. user: config.mysql.username,
  14. password: config.mysql.password,
  15. database: config.mysql.database,
  16. });
  17.  
  18. this.db.connect(function(err) {
  19. if(err)
  20. throw err;
  21. });
  22.  
  23. var self = this;
  24. this.app = http.createServer(handler);
  25. this.app.listen(config.port);
  26. this.io = io.listen(this.app);
  27. // this.io.sockets.on('connection', function(socket) {
  28. // self.connect_cb(socket);
  29. // });
  30.  
  31. self.pollingOptions.forEach(function(options, i) {
  32. setTimeout(function(i){
  33. self.pollingLoop(i);
  34. }, i*100, i)
  35. });
  36.  
  37. }
  38.  
  39. Server.prototype.connectionsArray = [];
  40. Server.prototype.pollingTimers = [];
  41. Server.prototype.pollingOptions = [
  42. {
  43. query: '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',
  44. notification_name: 'notification2',
  45. result_name: 'users_this',
  46. timer: 1000,
  47. },
  48. {
  49. query: 'SELECT * FROM time_option ORDER BY minutes',
  50. notification_name: 'notification3',
  51. result_name: 'option',
  52. timer: 1000,
  53. },
  54. {
  55. query: 'SELECT * FROM rates order by id desc limit 1',
  56. notification_name: 'notification4',
  57. result_name: 'rate',
  58. timer: 1000,
  59. },
  60. {
  61. query: '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 >= (UNIX_TIMESTAMP(CURRENT_TIMESTAMP)-86400) order by o.id desc',
  62. notification_name: 'notification',
  63. result_name: 'users',
  64. timer: 1000,
  65. }
  66. ];
  67.  
  68. function handler(req, res) {
  69. fs.readFile(__dirname + '/../index.php', function(err, data) {
  70. if (err) {
  71. console.log(err);
  72. res.writeHead(500);
  73. return res.end('Error loading index.php');
  74. }
  75. res.writeHead(200);
  76. res.end(data);
  77. });
  78. }
  79.  
  80. // creating a new websocket to keep the content updated without any AJAX request
  81. // Server.prototype.connect_cb = function (socket) {
  82. // var self = this;
  83. // if (!self.connectionsArray.length) {
  84. // self.pollingOptions.forEach(function(options, i) {
  85. // setTimeout(function(i){
  86. // self.pollingLoop(i);
  87. // }, i*100, i)
  88. // });
  89. // }
  90.  
  91. // socket.on('disconnect', function() {
  92. // var socketIndex = self.connectionsArray.indexOf(socket);
  93. // console.log('socketID = %s got disconnected', socketIndex);
  94. // if (~socketIndex) {
  95. // self.connectionsArray.splice(socketIndex, 1);
  96. // console.log(self.connectionsArray);
  97. // }
  98. // });
  99. // console.log('A new socket is connected!');
  100. // self.connectionsArray.push(socket);
  101. // // console.log(self.connectionsArray.length);
  102. // };
  103.  
  104. Server.prototype.setPollingLoop = function(option_id) {
  105. var self = this;
  106.  
  107. self.pollingTimers[option_id] = setTimeout(function() {
  108. self.pollingLoop(option_id)
  109. }, self.pollingOptions[option_id].timer);
  110. }
  111.  
  112. Server.prototype.pollingLoop = function(option_id) {
  113. var self = this;
  114. var options = self.pollingOptions[option_id];
  115.  
  116. if (!Object.keys(self.io.sockets.connected).length)
  117. return self.setPollingLoop(option_id);
  118.  
  119. self.db.query(options.query, function(err, rows) {
  120. if(err) {
  121. console.log(err);
  122. self.updateSockets(option_id, err);
  123. } else {
  124. self.updateSockets(option_id, rows);
  125. self.setPollingLoop(option_id);
  126. }
  127. });
  128. };
  129.  
  130. Server.prototype.updateSockets = function(option_id, result) {
  131. var self = this;
  132. var data = {
  133. time: new Date()
  134. };
  135. data[self.pollingOptions[option_id].result_name] = result;
  136.  
  137. // sending new data to all the sockets connected
  138. self.io.emit(self.pollingOptions[option_id].notification_name, data);
  139. };
  140.  
  141. module.exports = Server
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement