@@ -25,41 +25,94 @@ const CLOSING = 2;
2525const CLOSED = 3 ;
2626
2727
28- module . exports = W3CWebSocket ;
29-
30-
31- function W3CWebSocket ( url , protocols , origin , headers , requestOptions , clientConfig ) {
32- // Make this an EventTarget.
33- yaeti . EventTarget . call ( this ) ;
34-
35- // Sanitize clientConfig.
36- clientConfig = clientConfig || { } ;
37- clientConfig . assembleFragments = true ; // Required in the W3C API.
38-
39- const self = this ;
40-
41- this . _url = url ;
42- this . _readyState = CONNECTING ;
43- this . _protocol = undefined ;
44- this . _extensions = '' ;
45- this . _bufferedAmount = 0 ; // Hack, always 0.
46- this . _binaryType = 'arraybuffer' ; // TODO: Should be 'blob' by default, but Node has no Blob.
28+ class W3CWebSocket extends yaeti . EventTarget {
29+ constructor ( url , protocols , origin , headers , requestOptions , clientConfig ) {
30+ // Make this an EventTarget.
31+ super ( ) ;
32+
33+ // Sanitize clientConfig.
34+ clientConfig = clientConfig || { } ;
35+ clientConfig . assembleFragments = true ; // Required in the W3C API.
36+
37+ this . _url = url ;
38+ this . _readyState = CONNECTING ;
39+ this . _protocol = undefined ;
40+ this . _extensions = '' ;
41+ this . _bufferedAmount = 0 ; // Hack, always 0.
42+ this . _binaryType = 'arraybuffer' ; // TODO: Should be 'blob' by default, but Node has no Blob.
43+
44+ // The WebSocketConnection instance.
45+ this . _connection = undefined ;
46+
47+ // WebSocketClient instance.
48+ this . _client = new WebSocketClient ( clientConfig ) ;
49+
50+ this . _client . on ( 'connect' , ( connection ) => {
51+ onConnect . call ( this , connection ) ;
52+ } ) ;
4753
48- // The WebSocketConnection instance.
49- this . _connection = undefined ;
54+ this . _client . on ( 'connectFailed' , ( ) => {
55+ onConnectFailed . call ( this ) ;
56+ } ) ;
5057
51- // WebSocketClient instance.
52- this . _client = new WebSocketClient ( clientConfig ) ;
58+ this . _client . connect ( url , protocols , origin , headers , requestOptions ) ;
59+ }
5360
54- this . _client . on ( 'connect' , function ( connection ) {
55- onConnect . call ( self , connection ) ;
56- } ) ;
61+ send ( data ) {
62+ if ( this . _readyState !== OPEN ) {
63+ throw new Error ( 'cannot call send() while not connected' ) ;
64+ }
5765
58- this . _client . on ( 'connectFailed' , function ( ) {
59- onConnectFailed . call ( self ) ;
60- } ) ;
66+ // Text.
67+ if ( typeof data === 'string' || data instanceof String ) {
68+ this . _connection . sendUTF ( data ) ;
69+ }
70+ // Binary.
71+ else {
72+ // Node Buffer.
73+ if ( data instanceof Buffer ) {
74+ this . _connection . sendBytes ( data ) ;
75+ }
76+ // If ArrayBuffer or ArrayBufferView convert it to Node Buffer.
77+ else if ( data . byteLength || data . byteLength === 0 ) {
78+ data = toBuffer ( data ) ;
79+ this . _connection . sendBytes ( data ) ;
80+ }
81+ else {
82+ throw new Error ( 'unknown binary data:' , data ) ;
83+ }
84+ }
85+ }
6186
62- this . _client . connect ( url , protocols , origin , headers , requestOptions ) ;
87+ close ( code , reason ) {
88+ switch ( this . _readyState ) {
89+ case CONNECTING :
90+ // NOTE: We don't have the WebSocketConnection instance yet so no
91+ // way to close the TCP connection.
92+ // Artificially invoke the onConnectFailed event.
93+ onConnectFailed . call ( this ) ;
94+ // And close if it connects after a while.
95+ this . _client . on ( 'connect' , ( connection ) => {
96+ if ( code ) {
97+ connection . close ( code , reason ) ;
98+ } else {
99+ connection . close ( ) ;
100+ }
101+ } ) ;
102+ break ;
103+ case OPEN :
104+ this . _readyState = CLOSING ;
105+ if ( code ) {
106+ this . _connection . close ( code , reason ) ;
107+ } else {
108+ this . _connection . close ( ) ;
109+ }
110+ break ;
111+ case CLOSING :
112+ case CLOSED :
113+ break ;
114+ }
115+ }
63116}
64117
65118
@@ -106,64 +159,6 @@ Object.defineProperties(W3CWebSocket.prototype, {
106159} ) ;
107160
108161
109- W3CWebSocket . prototype . send = function ( data ) {
110- if ( this . _readyState !== OPEN ) {
111- throw new Error ( 'cannot call send() while not connected' ) ;
112- }
113-
114- // Text.
115- if ( typeof data === 'string' || data instanceof String ) {
116- this . _connection . sendUTF ( data ) ;
117- }
118- // Binary.
119- else {
120- // Node Buffer.
121- if ( data instanceof Buffer ) {
122- this . _connection . sendBytes ( data ) ;
123- }
124- // If ArrayBuffer or ArrayBufferView convert it to Node Buffer.
125- else if ( data . byteLength || data . byteLength === 0 ) {
126- data = toBuffer ( data ) ;
127- this . _connection . sendBytes ( data ) ;
128- }
129- else {
130- throw new Error ( 'unknown binary data:' , data ) ;
131- }
132- }
133- } ;
134-
135-
136- W3CWebSocket . prototype . close = function ( code , reason ) {
137- switch ( this . _readyState ) {
138- case CONNECTING :
139- // NOTE: We don't have the WebSocketConnection instance yet so no
140- // way to close the TCP connection.
141- // Artificially invoke the onConnectFailed event.
142- onConnectFailed . call ( this ) ;
143- // And close if it connects after a while.
144- this . _client . on ( 'connect' , function ( connection ) {
145- if ( code ) {
146- connection . close ( code , reason ) ;
147- } else {
148- connection . close ( ) ;
149- }
150- } ) ;
151- break ;
152- case OPEN :
153- this . _readyState = CLOSING ;
154- if ( code ) {
155- this . _connection . close ( code , reason ) ;
156- } else {
157- this . _connection . close ( ) ;
158- }
159- break ;
160- case CLOSING :
161- case CLOSED :
162- break ;
163- }
164- } ;
165-
166-
167162/**
168163 * Private API.
169164 */
@@ -257,3 +252,5 @@ function destroy() {
257252 this . _connection . removeAllListeners ( ) ;
258253 }
259254}
255+
256+ module . exports = W3CWebSocket ;
0 commit comments