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

Commit

Permalink
Merge pull request #7 from pderiy/master
Browse files Browse the repository at this point in the history
Allow to use SSL
  • Loading branch information
tlaverdure committed Aug 27, 2016
2 parents ab33d3c + c4cff27 commit c455b32
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
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("Server running 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

0 comments on commit c455b32

Please sign in to comment.