Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require('http')
  2.     , server = http.createServer()
  3.     , io = require('socket.io').listen(server);
  4.  
  5. /**
  6.  *
  7.  * @param http
  8.  * @param server
  9.  * @param io
  10.  */
  11. function Aplicacao(http, server, io) {
  12.     this.http = http;
  13.     this.server = server;
  14.     this.io = io;
  15.     this.portaConexaoPadrao = 3000;
  16.     this.fabricanteDeMensagens = new FabricanteDeMensagem(this.http, this.server, this.io);
  17.    
  18.     var _this = this;
  19.    
  20.     this.io.sockets.on('connection', function(client) {
  21.         client.on('send-server', function(mensagem) {
  22.             _this.recebeMensagem(mensagem);
  23.         });
  24.     });
  25.    
  26.     this.iniciar = function(portaConexao) {
  27.         if(portaConexao === undefined)
  28.             portaConexao = this.portaConexaoPadrao;
  29.        
  30.         this.server.listen(portaConexao, function() {
  31.             console.log('no ar...');
  32.         });
  33.     }  
  34.    
  35.     this.recebeMensagem = function(mensagem) {
  36.         if (mensagem.tipo !== undefined) {     
  37.             try {
  38.                 _this.fabricanteDeMensagens.fabricaMensagem(mensagem.tipo, function(mensagem) {
  39.                     mensagem.processa();
  40.                 });
  41.             } catch (e) {
  42.                 console.log(e);
  43.             }
  44.         } else {
  45.             // TODO: Tratar mensagens nao definidas aqui!
  46.         }
  47.     }
  48. }
  49.  
  50. /**
  51.  *
  52.  *
  53.  * @param http
  54.  * @param server
  55.  * @param io
  56.  */
  57. function FabricanteDeMensagem(http, server, io) {
  58.     this.http = http;
  59.     this.server = server;
  60.     this.io = io;
  61.    
  62.     var _this = this;
  63.    
  64.     this.fabricaMensagem = function(tipoDaMensagem, callback) {
  65.         this.formataNomeDoPrototype(tipoDaMensagem, function(nomeDoPrototype) {
  66.             prototype = nomeDoPrototype;
  67.             mensagem = eval('new ' + prototype + '(' + JSON.stringify(_this.http) + ')');
  68.             callback(mensagem);
  69.         });
  70.     }
  71.    
  72.     this.formataNomeDoPrototype = function(nomeDoPrototype, callback) {
  73.         var nomeDoPrototypeFormatado = '';
  74.        
  75.         partesDoNomeDoPrototype = nomeDoPrototype.split('-');
  76.        
  77.         for(posicaoNoArray in partesDoNomeDoPrototype) {
  78.             nomeDoPrototypeFormatado += partesDoNomeDoPrototype[posicaoNoArray].charAt(0).toUpperCase()
  79.                 + partesDoNomeDoPrototype[posicaoNoArray].substr(1);
  80.         }
  81.        
  82.         callback(nomeDoPrototypeFormatado);
  83.     }
  84. }
  85.  
  86.  
  87. /**
  88.  * Classe responsavel por autenticar usuarios na API
  89.  *
  90.  * @param http
  91.  *            object
  92.  * @return boolean|Usuario
  93.  */
  94. function Autenticador(http) {
  95.     this.http = http;
  96.     this.autenticaUsuario = function(tokenAutenticacao, callback) {
  97.  
  98.         var resultadoRequisicaoAPI = '';
  99.  
  100.         var requisicaoAPI = http.request({
  101.             host : 'localhost',
  102.             path : '/autenticacao.json?token=' + tokenAutenticacao,
  103.             method : 'GET'
  104.         }, function(retornoRequisicao) {
  105.             retornoRequisicao.on('data', function(dadosRetornados) {
  106.                 resultadoRequisicaoAPI += dadosRetornados;
  107.             });
  108.  
  109.             retornoRequisicao.on('end', function() {
  110.                 resultadoRequisicaoAPI = JSON.parse(resultadoRequisicaoAPI);
  111.  
  112.                 if (resultadoRequisicaoAPI.success === false)
  113.                     callback(false);
  114.                 else
  115.                     callback(resultadoRequisicaoAPI.usuario);
  116.             });
  117.         });
  118.  
  119.         requisicaoAPI.end();
  120.     }
  121. }
  122.  
  123. /**
  124.  * Interface para criacao de mensagens
  125.  *
  126.  * @param http
  127.  */
  128. function Mensagem(http) {
  129.     this.http = http;
  130.     this.processa = function() {
  131.         throw new Exception("Uninplemented method");
  132.     }
  133. }
  134.  
  135. /**
  136.  * Classe responsavel por emitir um sinal de presenca para outros usuarios
  137.  *
  138.  * @param http
  139.  */
  140. function MensagemPresenca(http) {
  141.     Mensagem.call(this, http);
  142.    
  143.     this.processa = function() {
  144.         console.log("processando...");
  145.     }
  146. }
  147.  
  148. MensagemPresenca.prototype = Object.create(MensagemPresenca.prototype);
  149. MensagemPresenca.prototype.constructor = MensagemPresenca;
  150.  
  151. var app = new Aplicacao(http, server, io);
  152. app.iniciar(3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement