4
4
*/
5
5
6
6
var http = require ( 'http' ) ,
7
+ https = require ( 'https' ) ,
7
8
faye = require ( 'faye' ) ,
8
9
server_auth = require ( './server-auth' ) ,
9
10
client_auth = require ( './client-auth' ) ,
11
+ sslConfig = require ( './ssl-config' ) ,
10
12
debug = require ( 'debug' ) ( 'openframe:pubsub' ) ,
11
13
12
14
// Exported object
13
15
pubsub = module . exports = { } ;
14
16
15
17
pubsub . connectedFrames = { } ;
16
18
17
- pubsub . start = function ( _port , _host ) {
19
+ pubsub . start = function ( _port , _host ) {
18
20
var port = _port || process . env . PS_PORT || '8889' ,
19
21
host = _host || process . env . PS_HOST || '0.0.0.0' ,
22
+ protocol = process . env . PS_HOST || 'http' ,
20
23
path = process . env . PS_PATH || '/faye' ,
21
24
api_url = process . env . API_EXPOSED_URL || 'http://0.0.0.0:8888/api' ,
22
- server = http . createServer ( ) ,
23
- bayeux = new faye . NodeAdapter ( { mount : path , timeout : 5 } ) ,
25
+ // server = http.createServer(),
26
+ bayeux = new faye . NodeAdapter ( { mount : path , timeout : 5 } ) ,
24
27
client = bayeux . getClient ( ) ;
25
28
29
+ let server = null ;
30
+ if ( protocol === 'https' ) {
31
+ const options = {
32
+ key : sslConfig . getPrivateKey ( ) ,
33
+ cert : sslConfig . getCertificate ( ) ,
34
+ } ;
35
+ server = https . createServer ( options ) ;
36
+ } else {
37
+ server = http . createServer ( ) ;
38
+ }
26
39
27
40
// add server_auth extension
28
41
bayeux . addExtension ( server_auth ( api_url ) ) ;
@@ -33,13 +46,13 @@ pubsub.start = function(_port, _host) {
33
46
/**
34
47
* Handle new client connection
35
48
*/
36
- bayeux . on ( 'handshake' , function ( clientId ) {
49
+ bayeux . on ( 'handshake' , function ( clientId ) {
37
50
debug ( 'new bayeux connection: ' , clientId ) ;
38
51
} ) ;
39
52
40
53
// When a client disconnects, see if it's in the hash of connected frames.
41
54
// If so, publish a disconnect event to any subscribed pubsub clients (e.g. the API server)
42
- bayeux . on ( 'disconnect' , function ( clientId ) {
55
+ bayeux . on ( 'disconnect' , function ( clientId ) {
43
56
debug ( 'bayeux connection closed: ' , clientId ) ;
44
57
if ( pubsub . connectedFrames [ clientId ] ) {
45
58
var frame_id = pubsub . connectedFrames [ clientId ] ;
@@ -54,7 +67,7 @@ pubsub.start = function(_port, _host) {
54
67
// add it to the list of connected frames
55
68
//
56
69
// also re-publish frame_id-namespaced connect event
57
- bayeux . on ( 'publish' , function ( clientId , channel , data ) {
70
+ bayeux . on ( 'publish' , function ( clientId , channel , data ) {
58
71
debug ( 'published' , channel , data ) ;
59
72
if ( channel === '/frame/connected' ) {
60
73
pubsub . connectedFrames [ clientId ] = data ;
@@ -70,11 +83,11 @@ pubsub.start = function(_port, _host) {
70
83
// Just some logging...
71
84
// TODO: log to file.
72
85
73
- client . subscribe ( '/frame/connected' , function ( data ) {
86
+ client . subscribe ( '/frame/connected' , function ( data ) {
74
87
debug ( '/frame/connected' , data ) ;
75
88
} ) ;
76
89
77
- client . subscribe ( '/frame/disconnected' , function ( data ) {
90
+ client . subscribe ( '/frame/disconnected' , function ( data ) {
78
91
debug ( '/frame/disconnected' , data ) ;
79
92
} ) ;
80
93
0 commit comments