Advertisement
Guest User

Untitled

a guest
Jul 31st, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 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._socket_io = io.listen(this.app);
  27. this._socket_io.sockets.on('connection', function(socket) {
  28. self.connect_cb(socket);
  29. });
  30. }
  31.  
  32. Server.prototype.connectionsArray = [];
  33. Server.prototype.pollingTimers = [];
  34. Server.prototype.pollingOptions = [
  35. {
  36. 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',
  37. notification_name: 'notification2',
  38. result_name: 'users_this',
  39. timer: 1000,
  40. },
  41. {
  42. query: 'SELECT * FROM time_option ORDER BY minutes',
  43. notification_name: 'notification3',
  44. result_name: 'option',
  45. timer: 1000,
  46. },
  47. {
  48. query: 'SELECT * FROM rates order by id desc limit 1',
  49. notification_name: 'notification4',
  50. result_name: 'rate',
  51. timer: 1000,
  52. },
  53. {
  54. 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',
  55. notification_name: 'notification',
  56. result_name: 'users',
  57. timer: 1000,
  58. }
  59. ];
  60.  
  61. function handler(req, res) {
  62. fs.readFile(__dirname + '/../index.php', function(err, data) {
  63. if (err) {
  64. console.log(err);
  65. res.writeHead(500);
  66. return res.end('Error loading index.php');
  67. }
  68. res.writeHead(200);
  69. res.end(data);
  70. });
  71. }
  72.  
  73. // creating a new websocket to keep the content updated without any AJAX request
  74. Server.prototype.connect_cb = function (socket) {
  75. var self = this;
  76. if (!self.connectionsArray.length) {
  77. self.pollingOptions.forEach(function(options, i) {
  78. setTimeout(function(i){
  79. self.pollingLoop(i);
  80. }, i*100, i)
  81. });
  82. }
  83.  
  84. socket.on('disconnect', function() {
  85. var socketIndex = self.connectionsArray.indexOf(socket);
  86. // console.log('socketID = %s got disconnected', socketIndex);
  87. if (~socketIndex) {
  88. self.connectionsArray.splice(socketIndex, 1);
  89. }
  90. });
  91. // console.log('A new socket is connected!');
  92. self.connectionsArray.push(socket);
  93. // console.log(self.connectionsArray.length);
  94. };
  95.  
  96. Server.prototype.pollingLoop = function(option_id) {
  97. var self = this;
  98. var options = self.pollingOptions[option_id];
  99. var query = self.db.query(options.query),
  100. result = []; // this array will contain the result of our db query
  101.  
  102. // if(option_id == 1) console.log('start '+option_id);
  103.  
  104. // setting the query listeners
  105. query.on('error', function(err) {
  106. // Handle error, and 'end' event will be emitted after this as well
  107. console.log(err);
  108. self.updateSockets(option_id, err);
  109. })
  110. .on('result', function(row) {
  111. // it fills our array looping on each user row inside the db
  112. result.push(row);
  113. })
  114. .on('end', function() {
  115. // loop on itself only if there are sockets still connected
  116. if (self.connectionsArray.length) {
  117. self.pollingTimers[option_id] = setTimeout(function() {
  118. self.pollingLoop(option_id)
  119. }, options.timer);
  120.  
  121. self.updateSockets(option_id, result);
  122. } else {
  123. console.log('The server timer was stopped because there are no more socket connections on the app')
  124. }
  125. });
  126. };
  127.  
  128. Server.prototype.updateSockets = function(option_id, result) {
  129. var self = this;
  130. var data = {
  131. time: new Date()
  132. };
  133. data[self.pollingOptions[option_id].result_name] = result;
  134.  
  135. // sending new data to all the sockets connected
  136. for (var i in self.connectionsArray) {
  137. self.connectionsArray[i].volatile.emit(self.pollingOptions[option_id].notification_name, data);
  138. };
  139. };
  140.  
  141. module.exports = Server
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement