summaryrefslogtreecommitdiff
path: root/node_modules/ws/examples/ssl.js
diff options
context:
space:
mode:
authoryo mama <pepper@scannerjammer.com>2015-03-20 17:33:43 -0700
committeryo mama <pepper@scannerjammer.com>2015-03-20 17:33:43 -0700
commit2afbcf4e7d000d99fdbc582d7113684ee00e80cc (patch)
tree6ba3313b78625735fb295e3d375e59054c31e558 /node_modules/ws/examples/ssl.js
first
Diffstat (limited to 'node_modules/ws/examples/ssl.js')
-rw-r--r--node_modules/ws/examples/ssl.js59
1 files changed, 59 insertions, 0 deletions
diff --git a/node_modules/ws/examples/ssl.js b/node_modules/ws/examples/ssl.js
new file mode 100644
index 0000000..bf1bf53
--- /dev/null
+++ b/node_modules/ws/examples/ssl.js
@@ -0,0 +1,59 @@
+
+(function(){
+
+ "use strict";
+
+ var fs = require('fs');
+
+ // you'll probably load configuration from config
+ var cfg = {
+ ssl: true,
+ port: 8080,
+ ssl_key: '/path/to/you/ssl.key',
+ ssl_cert: '/path/to/you/ssl.crt'
+ };
+
+ var httpServ = ( cfg.ssl ) ? require('https') : require('http');
+
+ var WebSocketServer = require('../').Server;
+
+ var app = null;
+
+ // dummy request processing
+ var processRequest = function( req, res ) {
+
+ res.writeHead(200);
+ res.end("All glory to WebSockets!\n");
+ };
+
+ if ( cfg.ssl ) {
+
+ app = httpServ.createServer({
+
+ // providing server with SSL key/cert
+ key: fs.readFileSync( cfg.ssl_key ),
+ cert: fs.readFileSync( cfg.ssl_cert )
+
+ }, processRequest ).listen( cfg.port );
+
+ } else {
+
+ app = httpServ.createServer( processRequest ).listen( cfg.port );
+ }
+
+ // passing or reference to web server so WS would knew port and SSL capabilities
+ var wss = new WebSocketServer( { server: app } );
+
+
+ wss.on( 'connection', function ( wsConnect ) {
+
+ wsConnect.on( 'message', function ( message ) {
+
+ console.log( message );
+
+ });
+
+ });
+
+
+}()); \ No newline at end of file