@@ -43,16 +43,15 @@ class WebSocket extends EventEmitter {
43
43
constructor ( address , protocols , options ) {
44
44
super ( ) ;
45
45
46
- this . readyState = WebSocket . CONNECTING ;
47
- this . protocol = '' ;
48
-
49
46
this . _binaryType = BINARY_TYPES [ 0 ] ;
47
+ this . _closeCode = 1006 ;
50
48
this . _closeFrameReceived = false ;
51
49
this . _closeFrameSent = false ;
52
50
this . _closeMessage = '' ;
53
51
this . _closeTimer = null ;
54
- this . _closeCode = 1006 ;
55
52
this . _extensions = { } ;
53
+ this . _protocol = '' ;
54
+ this . _readyState = WebSocket . CONNECTING ;
56
55
this . _receiver = null ;
57
56
this . _sender = null ;
58
57
this . _socket = null ;
@@ -126,6 +125,27 @@ class WebSocket extends EventEmitter {
126
125
return Object . keys ( this . _extensions ) . join ( ) ;
127
126
}
128
127
128
+ /**
129
+ * @type {String }
130
+ */
131
+ get protocol ( ) {
132
+ return this . _protocol ;
133
+ }
134
+
135
+ /**
136
+ * @type {Number }
137
+ */
138
+ get readyState ( ) {
139
+ return this . _readyState ;
140
+ }
141
+
142
+ /**
143
+ * @type {String }
144
+ */
145
+ get url ( ) {
146
+ return this . _url ;
147
+ }
148
+
129
149
/**
130
150
* Set up the socket and the internal resources.
131
151
*
@@ -166,7 +186,7 @@ class WebSocket extends EventEmitter {
166
186
socket . on ( 'end' , socketOnEnd ) ;
167
187
socket . on ( 'error' , socketOnError ) ;
168
188
169
- this . readyState = WebSocket . OPEN ;
189
+ this . _readyState = WebSocket . OPEN ;
170
190
this . emit ( 'open' ) ;
171
191
}
172
192
@@ -177,7 +197,7 @@ class WebSocket extends EventEmitter {
177
197
*/
178
198
emitClose ( ) {
179
199
if ( ! this . _socket ) {
180
- this . readyState = WebSocket . CLOSED ;
200
+ this . _readyState = WebSocket . CLOSED ;
181
201
this . emit ( 'close' , this . _closeCode , this . _closeMessage ) ;
182
202
return ;
183
203
}
@@ -187,7 +207,7 @@ class WebSocket extends EventEmitter {
187
207
}
188
208
189
209
this . _receiver . removeAllListeners ( ) ;
190
- this . readyState = WebSocket . CLOSED ;
210
+ this . _readyState = WebSocket . CLOSED ;
191
211
this . emit ( 'close' , this . _closeCode , this . _closeMessage ) ;
192
212
}
193
213
@@ -222,7 +242,7 @@ class WebSocket extends EventEmitter {
222
242
return ;
223
243
}
224
244
225
- this . readyState = WebSocket . CLOSING ;
245
+ this . _readyState = WebSocket . CLOSING ;
226
246
this . _sender . close ( code , data , ! this . _isServer , ( err ) => {
227
247
//
228
248
// This error is handled by the `'error'` listener on the socket. We only
@@ -367,14 +387,17 @@ class WebSocket extends EventEmitter {
367
387
}
368
388
369
389
if ( this . _socket ) {
370
- this . readyState = WebSocket . CLOSING ;
390
+ this . _readyState = WebSocket . CLOSING ;
371
391
this . _socket . destroy ( ) ;
372
392
}
373
393
}
374
394
}
375
395
376
396
readyStates . forEach ( ( readyState , i ) => {
377
- WebSocket [ readyState ] = i ;
397
+ Object . defineProperty ( WebSocket , readyState , {
398
+ enumerable : true ,
399
+ value : i
400
+ } ) ;
378
401
} ) ;
379
402
380
403
//
@@ -474,10 +497,10 @@ function initAsClient(websocket, address, protocols, options) {
474
497
475
498
if ( address instanceof URL ) {
476
499
parsedUrl = address ;
477
- websocket . url = address . href ;
500
+ websocket . _url = address . href ;
478
501
} else {
479
502
parsedUrl = new URL ( address ) ;
480
- websocket . url = address ;
503
+ websocket . _url = address ;
481
504
}
482
505
483
506
const isUnixSocket = parsedUrl . protocol === 'ws+unix:' ;
@@ -552,7 +575,7 @@ function initAsClient(websocket, address, protocols, options) {
552
575
if ( websocket . _req . aborted ) return ;
553
576
554
577
req = websocket . _req = null ;
555
- websocket . readyState = WebSocket . CLOSING ;
578
+ websocket . _readyState = WebSocket . CLOSING ;
556
579
websocket . emit ( 'error' , err ) ;
557
580
websocket . emitClose ( ) ;
558
581
} ) ;
@@ -623,7 +646,7 @@ function initAsClient(websocket, address, protocols, options) {
623
646
return ;
624
647
}
625
648
626
- if ( serverProt ) websocket . protocol = serverProt ;
649
+ if ( serverProt ) websocket . _protocol = serverProt ;
627
650
628
651
if ( perMessageDeflate ) {
629
652
try {
@@ -688,7 +711,7 @@ function tlsConnect(options) {
688
711
* @private
689
712
*/
690
713
function abortHandshake ( websocket , stream , message ) {
691
- websocket . readyState = WebSocket . CLOSING ;
714
+ websocket . _readyState = WebSocket . CLOSING ;
692
715
693
716
const err = new Error ( message ) ;
694
717
Error . captureStackTrace ( err , abortHandshake ) ;
@@ -777,7 +800,7 @@ function receiverOnError(err) {
777
800
778
801
websocket . _socket . removeListener ( 'data' , socketOnData ) ;
779
802
780
- websocket . readyState = WebSocket . CLOSING ;
803
+ websocket . _readyState = WebSocket . CLOSING ;
781
804
websocket . _closeCode = err [ kStatusCode ] ;
782
805
websocket . emit ( 'error' , err ) ;
783
806
websocket . _socket . destroy ( ) ;
@@ -836,7 +859,7 @@ function socketOnClose() {
836
859
this . removeListener ( 'close' , socketOnClose ) ;
837
860
this . removeListener ( 'end' , socketOnEnd ) ;
838
861
839
- websocket . readyState = WebSocket . CLOSING ;
862
+ websocket . _readyState = WebSocket . CLOSING ;
840
863
841
864
//
842
865
// The close frame might not have been received or the `'end'` event emitted,
@@ -887,7 +910,7 @@ function socketOnData(chunk) {
887
910
function socketOnEnd ( ) {
888
911
const websocket = this [ kWebSocket ] ;
889
912
890
- websocket . readyState = WebSocket . CLOSING ;
913
+ websocket . _readyState = WebSocket . CLOSING ;
891
914
websocket . _receiver . end ( ) ;
892
915
this . end ( ) ;
893
916
}
@@ -904,7 +927,7 @@ function socketOnError() {
904
927
this . on ( 'error' , NOOP ) ;
905
928
906
929
if ( websocket ) {
907
- websocket . readyState = WebSocket . CLOSING ;
930
+ websocket . _readyState = WebSocket . CLOSING ;
908
931
this . destroy ( ) ;
909
932
}
910
933
}
0 commit comments