Skip to content

Commit

Permalink
seccion 4
Browse files Browse the repository at this point in the history
  • Loading branch information
hamundnic committed Jan 13, 2019
1 parent 47e7949 commit 6543426
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 13 deletions.
35 changes: 30 additions & 5 deletions classes/server.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
import express from 'express';
import { SERVER_PORT } from '../global/environment';
import socketIO from 'socket.io';
import http from 'http';
import * as socket from '../sockets/socket';



export default class Server{

private static _instance:Server;
public app:express.Application;
public port:number;

constructor(){
public io:socketIO.Server;
private httpServer:http.Server;
private constructor(){
this.app=express();
this.port=SERVER_PORT;
this.httpServer= new http.Server(this.app);
this.io = socketIO(this.httpServer);
this.escucharSockets();

}
start(callback:Function){
this.app.listen(this.port,callback);

public static get instance(){
return this._instance || (this._instance = new this());
}

private escucharSockets(){
console.log('escuchando conecciones - Sockets');
this.io.on('connection',cliente=>{
console.log('Cliente conectado');
// Mensaje
socket.mensaje(cliente,this.io);
// desconectar
socket.desconectar(cliente);

})
}
start(callback:Function){
// this.app.listen(this.port,callback);
this.httpServer.listen(this.port,callback);

}
}
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Server from './classes/server';
import { SERVER_PORT } from './global/environment';
//import { SERVER_PORT } from './global/environment';
import router from './routes/router';
import bodyParser from 'body-parser';
import cors from 'cors';

const server =new Server();
const server =Server.instance;

//bodyParser
server.app.use(bodyParser.urlencoded({extended:true}));
Expand All @@ -20,5 +20,5 @@ server.app.use(cors({
server.app.use('/',router)

server.start(()=>{
console.log(`Servidor esta corriendo en el puerto ${SERVER_PORT}`)
console.log(`Servidor esta corriendo en el puerto ${server.port}`)
})
Loading

0 comments on commit 6543426

Please sign in to comment.