Created
December 20, 2017 21:48
-
-
Save luixaviles/f6815350bc93ab071e4a806d469c9647 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createServer, Server } from 'http'; | |
import * as express from 'express'; | |
import * as socketIo from 'socket.io'; | |
import { Message } from './model'; | |
export class ChatServer { | |
public static readonly PORT:number = 8080; | |
private app: express.Application; | |
private server: Server; | |
private io: SocketIO.Server; | |
private port: string | number; | |
constructor() { | |
this.createApp(); | |
this.config(); | |
this.createServer(); | |
this.sockets(); | |
this.listen(); | |
} | |
private createApp(): void { | |
this.app = express(); | |
} | |
private createServer(): void { | |
this.server = createServer(this.app); | |
} | |
private config(): void { | |
this.port = process.env.PORT || ChatServer.PORT; | |
} | |
private sockets(): void { | |
this.io = socketIo(this.server); | |
} | |
private listen(): void { | |
this.server.listen(this.port, () => { | |
console.log('Running server on port %s', this.port); | |
}); | |
this.io.on('connect', (socket: any) => { | |
console.log('Connected client on port %s.', this.port); | |
socket.on('message', (m: Message) => { | |
console.log('[server](message): %s', JSON.stringify(m)); | |
this.io.emit('message', m); | |
}); | |
socket.on('disconnect', () => { | |
console.log('Client disconnected'); | |
}); | |
}); | |
} | |
public getApp(): express.Application { | |
return this.app; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment