Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict;'
- var io = require('socket.io');
- var fs = require('fs');
- var mysql = require('mysql');
- var http = require('http');
- var config = require("./config");
- function Server() {
- var self = this;
- this.db = mysql.createConnection({
- host: 'localhost',
- user: config.mysql.username,
- password: config.mysql.password,
- database: config.mysql.database,
- });
- this.db.connect(function(err) {
- if(err)
- throw err;
- });
- var self = this;
- this.app = http.createServer(handler);
- this.app.listen(config.port);
- this._socket_io = io.listen(this.app);
- this._socket_io.sockets.on('connection', function(socket) {
- self.connect_cb(socket);
- });
- }
- Server.prototype.connectionsArray = [];
- Server.prototype.pollingTimers = [];
- Server.prototype.pollingOptions = [
- {
- id: 1,
- 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',
- notification_name: 'notification',
- result_name: 'users',
- timer: 1000,
- },
- {
- id: 2,
- 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',
- notification_name: 'notification2',
- result_name: 'users_this',
- timer: 1000,
- },
- {
- id: 3,
- query: 'SELECT * FROM time_option ORDER BY minutes',
- notification_name: 'notification3',
- result_name: 'option',
- timer: 1000,
- },
- {
- id: 4,
- query: 'SELECT * FROM rates order by id desc limit 1',
- notification_name: 'notification4',
- result_name: 'rate',
- timer: 2000,
- }
- ];
- 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);
- });
- }
- // creating a new websocket to keep the content updated without any AJAX request
- Server.prototype.connect_cb = function (socket) {
- var self = this;
- if (!connectionsArray.length) {
- self.pollingOptions.forEach(function(options) {
- self.pollingLoop(options.id)
- });
- }
- socket.on('disconnect', function() {
- var socketIndex = self.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);
- };
- Server.prototype.pollingLoop = function(option_id) {
- var self = this;
- var options = self.pollingOptions[option_id];
- var query = self.db.query(options.query),
- result = []; // 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);
- self.updateSockets(options.notification_name, err);
- })
- .on('result', function(user) {
- // it fills our array looping on each user row inside the db
- result.push(result);
- })
- .on('end', function() {
- // loop on itself only if there are sockets still connected
- if (self.connectionsArray.length) {
- self.pollingTimers[options.id] = setTimeout(function() {
- self.pollingLoop(option_id)
- }, options.timer);
- self.updateSockets(options.id, result);
- } else {
- console.log('The server timer was stopped because there are no more socket connections on the app')
- }
- });
- };
- Server.prototype.updateSockets = function(option_id, result) {
- var self = this;
- var data = {
- time: new Date()
- };
- data[self.pollingOptions[option_id].result_name] = result;
- // sending new data to all the sockets connected
- self.connectionsArray.forEach(function(tmpSocket) {
- tmpSocket.volatile.emit(self.pollingOptions[option_id].notification_name, data);
- });
- };
- module.exports = Server
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement