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.io = io.listen(this.app);
- // this.io.sockets.on('connection', function(socket) {
- // self.connect_cb(socket);
- // });
- self.pollingOptions.forEach(function(options, i) {
- setTimeout(function(i){
- self.pollingLoop(i);
- }, i*100, i)
- });
- }
- Server.prototype.connectionsArray = [];
- Server.prototype.pollingTimers = [];
- Server.prototype.pollingOptions = [
- {
- 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,
- },
- {
- query: 'SELECT * FROM time_option ORDER BY minutes',
- notification_name: 'notification3',
- result_name: 'option',
- timer: 1000,
- },
- {
- query: 'SELECT * FROM rates order by id desc limit 1',
- notification_name: 'notification4',
- result_name: 'rate',
- timer: 1000,
- },
- {
- 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',
- notification_name: 'notification',
- result_name: 'users',
- timer: 1000,
- }
- ];
- 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 (!self.connectionsArray.length) {
- // self.pollingOptions.forEach(function(options, i) {
- // setTimeout(function(i){
- // self.pollingLoop(i);
- // }, i*100, i)
- // });
- // }
- // socket.on('disconnect', function() {
- // var socketIndex = self.connectionsArray.indexOf(socket);
- // console.log('socketID = %s got disconnected', socketIndex);
- // if (~socketIndex) {
- // self.connectionsArray.splice(socketIndex, 1);
- // console.log(self.connectionsArray);
- // }
- // });
- // console.log('A new socket is connected!');
- // self.connectionsArray.push(socket);
- // // console.log(self.connectionsArray.length);
- // };
- Server.prototype.setPollingLoop = function(option_id) {
- var self = this;
- self.pollingTimers[option_id] = setTimeout(function() {
- self.pollingLoop(option_id)
- }, self.pollingOptions[option_id].timer);
- }
- Server.prototype.pollingLoop = function(option_id) {
- var self = this;
- var options = self.pollingOptions[option_id];
- if (!Object.keys(self.io.sockets.connected).length)
- return self.setPollingLoop(option_id);
- self.db.query(options.query, function(err, rows) {
- if(err) {
- console.log(err);
- self.updateSockets(option_id, err);
- } else {
- self.updateSockets(option_id, rows);
- self.setPollingLoop(option_id);
- }
- });
- };
- 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.io.emit(self.pollingOptions[option_id].notification_name, data);
- };
- module.exports = Server
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement