Stream data with socket.io

4

Hello, I'm streaming data from the bank in this way, I know it's too overloaded by the client's browser, would I have another way to stream it without loading it?

var express = require('express'),   
    app = express(), 
    session = require('express-session')
    http = require('http').Server(app),
    io = require('socket.io')(http),
    dbasy = require('./node_modules/dbasy/dbasy.js'),
    $ = require('cheerio'),
    jsdom = require("jsdom"),
    request = require('request'),
    _ = require('underscore')._,
    url = require('url');

var LRU = require("lru-cache")
  , options = { max: 200
              , length: function (n) { return n * 2 }
              , dispose: function (key, n) { n.close() }
              , maxAge: 1000 * 60 * 60 }
  , cache = LRU(options)
  , otherCache = LRU(50) ;// sets just the max size


cache.reset() ;   // empty the cache

app.use(require('express').static(__dirname));

app.get('/', function(req, res) {

    res.sendFile(__dirname + '/index.htm');

});

var clients = 0, data,data_nova, data_antiga, upd, setores = '',acess;


io.sockets.on('connect', function(socket) { clients++;


    socket.on('log', function(log){
        clearInterval(upd);
        setores = '';   
        console.log(log);
        request('http://localhost/node/dashponto/php/index.php', function(error, response, body){
            var dds = JSON.parse(body);
            var wr  = _.where(dds,{funcionario_cracha :log[0],  funcionario_cpf : log[1]});



                if(wr.length >= 1){
                _.each(wr[0].operacoes, function(o,p){
                    console.log('Setor liberado:'+p);
                    setores += '<option value="'+p+'">'+p+'</option>';

                });

                    socket.emit('setores',setores);     
                    socket.emit('retorno-setores-status', dds);

                    start();


                }else if(log[0] == 'master' && log[1] == '123456'){

                    _.each(dds['oper'], function(b,n){              

                        setores += '<option value="'+b+'">'+b+'</option>';

                    });

                    socket.emit('setores',setores);     
                    socket.emit('retorno-setores-status', dds);

                    start();

                }else{ socket.emit('setores',''); }



        });

    console.log(setores);               


    });

    socket.on('disconnect', function() { clients--; if(clients == 0){ ci();} }); });

function dashponto() {

        console.log('Clientes conectados: '+clients);
        request('http://localhost/node/dashponto/php/index.php', function(error, response, body){

            data_nova = body;

            if(data_nova != data_antiga){

            console.log('Dados atualizados - '+new Date());

            data = JSON.parse(data_nova)

            // setores = '<option value="">Selecione o setor ou operação</option>'; 


            //socket.emit('setores',setores);
            //socket.emit('retorno-setores-status', data);

            //io.sockets.emit('setores',setores);           
            io.sockets.emit('retorno-setores-status', data);

            //socket.broadcast.emit('setores',setores);
            //io.sockets.broadcast.emit('retorno-setores-status', data);
            data_antiga = data_nova;    


            }else{

                console.log('Dados iguais');


            }



    });



  };

//START 

function start(){

        upd = setInterval(dashponto,5000);

    }   


//

function ci(){  
    clearInterval(upd);
}



http.listen(1589, function(){

    console.log('[Servidor iniciado]');


});
    
asked by anonymous 02.01.2015 / 13:25

1 answer

-1
unbind()

The problem was cumulative events

    
23.03.2015 / 21:58