21
21
*/
22
22
23
23
var net = require ( 'net' ) ,
24
+ tls = require ( 'tls' ) ,
24
25
sys = require ( 'sys' ) ,
25
26
frame = require ( './frame' ) ,
26
27
stomp_utils = require ( './stomp-utils' ) ,
@@ -91,21 +92,41 @@ function parse_frame(chunk) {
91
92
} ;
92
93
93
94
function _connect ( stomp ) {
94
- var socket = stomp . socket = net . Stream ( ) ;
95
95
log = stomp . log ;
96
- log . debug ( 'Connecting to ' + stomp . host + ':' + stomp . port ) ;
97
- socket . connect ( stomp . port , stomp . host ) ;
98
96
99
- socket . on ( 'connect' , function ( ) {
97
+ if ( stomp . ssl ) {
98
+ log . debug ( 'Connecting to ' + stomp . host + ':' + stomp . port + ' using SSL' ) ;
99
+ stomp . socket = tls . connect ( stomp . port , stomp . host , stomp . ssl_options , function ( ) {
100
+ log . debug ( 'SSL connection complete' ) ;
101
+ if ( ! stomp . socket . authorized ) {
102
+ log . error ( 'SSL is not authorized: ' + stomp . socket . authorizationError ) ;
103
+ if ( stomp . ssl_validate ) {
104
+ _disconnect ( stomp ) ;
105
+ return ;
106
+ }
107
+ }
108
+ _setupListeners ( stomp ) ;
109
+ } ) ;
110
+ } else {
111
+ log . debug ( 'Connecting to ' + stomp . host + ':' + stomp . port ) ;
112
+ stomp . socket = new net . Socket ( ) ;
113
+ stomp . socket . connect ( stomp . port , stomp . host ) ;
114
+ _setupListeners ( stomp ) ;
115
+ }
116
+ }
117
+
118
+ function _setupListeners ( stomp ) {
119
+ function _connected ( ) {
100
120
log . debug ( 'Connected to socket' ) ;
101
121
if ( utils . really_defined ( stomp . login ) &&
102
122
utils . really_defined ( stomp . passcode ) ) {
103
123
stomp_connect ( stomp , { login : stomp . login , passcode : stomp . passcode } ) ;
104
- }
105
- else {
124
+ } else {
106
125
stomp_connect ( stomp ) ;
107
126
}
108
- } ) ;
127
+ }
128
+
129
+ var socket = stomp . socket ;
109
130
110
131
socket . on ( 'drain' , function ( data ) {
111
132
log . debug ( 'draining' ) ;
@@ -122,10 +143,9 @@ function _connect(stomp) {
122
143
var parsed_frame = null ;
123
144
var _frame = null ;
124
145
while ( _frame = frames . shift ( ) ) {
125
- parsed_frame = parse_frame ( _frame )
146
+ parsed_frame = parse_frame ( _frame ) ;
126
147
stomp . handle_new_frame ( parsed_frame ) ;
127
148
}
128
-
129
149
} ) ;
130
150
131
151
socket . on ( 'end' , function ( ) {
@@ -142,6 +162,12 @@ function _connect(stomp) {
142
162
log . error ( 'Disconnected with error: ' + error ) ;
143
163
stomp . emit ( "disconnected" ) ;
144
164
} ) ;
165
+
166
+ if ( stomp . ssl ) {
167
+ _connected ( ) ;
168
+ } else {
169
+ socket . on ( 'connect' , _connected ) ;
170
+ }
145
171
} ;
146
172
147
173
function stomp_connect ( stomp , headers ) {
@@ -155,7 +181,6 @@ function stomp_connect(stomp, headers) {
155
181
var frame_to_send = _frame . build_frame ( args ) ;
156
182
157
183
send_frame ( stomp , frame_to_send ) ;
158
- log . debug ( 'Connected to STOMP' ) ;
159
184
} ;
160
185
161
186
function _disconnect ( stomp ) {
@@ -208,6 +233,9 @@ function Stomp(args) {
208
233
this . log = new StompLogging ( this . debug ) ;
209
234
this . _subscribed_to = { } ;
210
235
this . session = null ;
236
+ this . ssl = args [ 'ssl' ] ? true : false ;
237
+ this . ssl_validate = args [ 'ssl_validate' ] ? true : false ;
238
+ this . ssl_options = args [ 'ssl_options' ] || [ ] ;
211
239
} ;
212
240
213
241
Stomp . prototype = new process . EventEmitter ( ) ;
@@ -234,6 +262,7 @@ Stomp.prototype.handle_new_frame = function(this_frame) {
234
262
self . emit ( 'message' , this_frame ) ;
235
263
break ;
236
264
case "CONNECTED" :
265
+ log . debug ( 'Connected to STOMP' ) ;
237
266
self . session = this_frame . headers [ 'session' ] ;
238
267
self . emit ( 'connected' ) ;
239
268
break ;
0 commit comments