Advertisement
Guest User

Untitled

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