Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http')
- , server = http.createServer()
- , io = require('socket.io').listen(server);
- /**
- *
- * @param http
- * @param server
- * @param io
- */
- function Aplicacao(http, server, io) {
- this.http = http;
- this.server = server;
- this.io = io;
- this.portaConexaoPadrao = 3000;
- this.fabricanteDeMensagens = new FabricanteDeMensagem(this.http, this.server, this.io);
- var _this = this;
- this.io.sockets.on('connection', function(client) {
- client.on('send-server', function(mensagem) {
- _this.recebeMensagem(mensagem);
- });
- });
- this.iniciar = function(portaConexao) {
- if(portaConexao === undefined)
- portaConexao = this.portaConexaoPadrao;
- this.server.listen(portaConexao, function() {
- console.log('no ar...');
- });
- }
- this.recebeMensagem = function(mensagem) {
- if (mensagem.tipo !== undefined) {
- try {
- _this.fabricanteDeMensagens.fabricaMensagem(mensagem.tipo, function(mensagem) {
- mensagem.processa();
- });
- } catch (e) {
- console.log(e);
- }
- } else {
- // TODO: Tratar mensagens nao definidas aqui!
- }
- }
- }
- /**
- *
- *
- * @param http
- * @param server
- * @param io
- */
- function FabricanteDeMensagem(http, server, io) {
- this.http = http;
- this.server = server;
- this.io = io;
- var _this = this;
- this.fabricaMensagem = function(tipoDaMensagem, callback) {
- this.formataNomeDoPrototype(tipoDaMensagem, function(nomeDoPrototype) {
- prototype = nomeDoPrototype;
- mensagem = eval('new ' + prototype + '(' + JSON.stringify(_this.http) + ')');
- callback(mensagem);
- });
- }
- this.formataNomeDoPrototype = function(nomeDoPrototype, callback) {
- var nomeDoPrototypeFormatado = '';
- partesDoNomeDoPrototype = nomeDoPrototype.split('-');
- for(posicaoNoArray in partesDoNomeDoPrototype) {
- nomeDoPrototypeFormatado += partesDoNomeDoPrototype[posicaoNoArray].charAt(0).toUpperCase()
- + partesDoNomeDoPrototype[posicaoNoArray].substr(1);
- }
- callback(nomeDoPrototypeFormatado);
- }
- }
- /**
- * Classe responsavel por autenticar usuarios na API
- *
- * @param http
- * object
- * @return boolean|Usuario
- */
- function Autenticador(http) {
- this.http = http;
- this.autenticaUsuario = function(tokenAutenticacao, callback) {
- var resultadoRequisicaoAPI = '';
- var requisicaoAPI = http.request({
- host : 'localhost',
- path : '/autenticacao.json?token=' + tokenAutenticacao,
- method : 'GET'
- }, function(retornoRequisicao) {
- retornoRequisicao.on('data', function(dadosRetornados) {
- resultadoRequisicaoAPI += dadosRetornados;
- });
- retornoRequisicao.on('end', function() {
- resultadoRequisicaoAPI = JSON.parse(resultadoRequisicaoAPI);
- if (resultadoRequisicaoAPI.success === false)
- callback(false);
- else
- callback(resultadoRequisicaoAPI.usuario);
- });
- });
- requisicaoAPI.end();
- }
- }
- /**
- * Interface para criacao de mensagens
- *
- * @param http
- */
- function Mensagem(http) {
- this.http = http;
- this.processa = function() {
- throw new Exception("Uninplemented method");
- }
- }
- /**
- * Classe responsavel por emitir um sinal de presenca para outros usuarios
- *
- * @param http
- */
- function MensagemPresenca(http) {
- Mensagem.call(this, http);
- this.processa = function() {
- console.log("processando...");
- }
- }
- MensagemPresenca.prototype = Object.create(MensagemPresenca.prototype);
- MensagemPresenca.prototype.constructor = MensagemPresenca;
- var app = new Aplicacao(http, server, io);
- app.iniciar(3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement