comunicationLeap/node_modules/ws/examples/ssl.js
node_modules/ws/examples/ssl.jsRaw
1
2(function(){
3
4 "use strict";
5
6 var fs = require('fs');
7
8 // you'll probably load configuration from config
9 var cfg = {
10 ssl: true,
11 port: 8080,
12 ssl_key: '/path/to/you/ssl.key',
13 ssl_cert: '/path/to/you/ssl.crt'
14 };
15
16 var httpServ = ( cfg.ssl ) ? require('https') : require('http');
17
18 var WebSocketServer = require('../').Server;
19
20 var app = null;
21
22 // dummy request processing
23 var processRequest = function( req, res ) {
24
25 res.writeHead(200);
26 res.end("All glory to WebSockets!\n");
27 };
28
29 if ( cfg.ssl ) {
30
31 app = httpServ.createServer({
32
33 // providing server with SSL key/cert
34 key: fs.readFileSync( cfg.ssl_key ),
35 cert: fs.readFileSync( cfg.ssl_cert )
36
37 }, processRequest ).listen( cfg.port );
38
39 } else {
40
41 app = httpServ.createServer( processRequest ).listen( cfg.port );
42 }
43
44 // passing or reference to web server so WS would knew port and SSL capabilities
45 var wss = new WebSocketServer( { server: app } );
46
47
48 wss.on( 'connection', function ( wsConnect ) {
49
50 wsConnect.on( 'message', function ( message ) {
51
52 console.log( message );
53
54 });
55
56 });
57
58
59}());