Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Allow to use SSL #7

Merged
merged 3 commits into from
Aug 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ echo.run(options);
| `authPath` | `/broadcasting/auth` | The route that authenticates private channels |
| `host` | `http://localhost` | The host of the socket.io server |
| `port` | `6001` | The port that the socket.io server should run on |

| `https` | `boolean` | Boolean that allows to use SSL |
| `ssl_key_path` | `string` | The path to your client ssl key |
| `ssl_cert_path` | `string` | The path to your client ssl certificate |

## Client Side Configuration

Expand Down
33 changes: 32 additions & 1 deletion src/echo-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ let _ = require('lodash');
let io = require('socket.io')
let Redis = require('ioredis')
let request = require('request')
let https = require('https');

/**
* Echo server class.
Expand Down Expand Up @@ -80,18 +81,48 @@ export class EchoServer {
*/
run(options: any): void {
this.options = _.merge(this._options, options);
this.loadSecureMode();
this.initializeSocketIo();
this.startSocketIoServer();
this.redisPubSub();
this.log("Servering at " + this.options.host + ":" + this.options.port);
}

/**
* Load SSL 'key' & 'cert' files if https is enabled
*
* @return {void}
*/
loadSecureMode() {
if (!this.options.https) return;

_.assignIn(this.options, {
key: fs.readFileSync(this.options.ssl_key_path),
cert: fs.readFileSync(this.options.ssl_cert_path)
});
}

/**
* Initialize socket.io variable
*/
initializeSocketIo() {

if (!this.options.https) {
this._io = io(this.options.port);
return;
}

let _https = https.createServer(this.options).listen(this.options.port);

this._io = io(_https);
}

/**
* Start the Socket.io server.
*
* @return {void}
*/
startSocketIoServer(): void {
this._io = io(this.options.port);
this._io.on('connection', socket => {
this.onSubscribe(socket);
this.onUnsubscribe(socket);
Expand Down