@@ -76,9 +76,9 @@ export class WebSocket
76
76
77
77
binaryType : 'arraybuffer' | 'blob' = 'blob'
78
78
79
- private _readyState : WebSocketReadyState = WebSocketReadyState . CONNECTING
79
+ #readyState : WebSocketReadyState = WebSocketReadyState . CONNECTING
80
80
get readyState ( ) {
81
- return this . _readyState
81
+ return this . #readyState
82
82
}
83
83
84
84
get bufferedAmount ( ) {
@@ -89,9 +89,9 @@ export class WebSocket
89
89
throw new Error ( 'Not implemented' )
90
90
}
91
91
92
- private _protocol = ''
92
+ #protocol = ''
93
93
get protocol ( ) {
94
- return this . _protocol
94
+ return this . #protocol
95
95
}
96
96
97
97
private readonly ws : HybridWebSocket
@@ -103,8 +103,8 @@ export class WebSocket
103
103
this . ws = WebSocketManager . create ( url , Array . isArray ( protocols ) ? protocols : [ protocols ] )
104
104
105
105
this . ws . onOpen ( ( protocol ) => {
106
- this . _readyState = WebSocketReadyState . OPEN
107
- this . _protocol = protocol
106
+ this . #readyState = WebSocketReadyState . OPEN
107
+ this . #protocol = protocol
108
108
this . dispatchEvent ( new Event ( 'open' ) )
109
109
} )
110
110
@@ -127,14 +127,14 @@ export class WebSocket
127
127
* Sending `close` frame before proceeding to close the connection
128
128
* https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.7
129
129
*/
130
- this . _readyState = WebSocketReadyState . CLOSED
130
+ this . #readyState = WebSocketReadyState . CLOSED
131
131
this . dispatchEvent ( new CloseEvent ( ABNORMAL_CLOSURE ) )
132
132
133
133
this . close ( )
134
134
} )
135
135
136
136
this . ws . onClose ( ( code , reason ) => {
137
- this . _readyState = WebSocketReadyState . CLOSED
137
+ this . #readyState = WebSocketReadyState . CLOSED
138
138
this . dispatchEvent ( new CloseEvent ( code , reason ) )
139
139
} )
140
140
@@ -146,10 +146,14 @@ export class WebSocket
146
146
*/
147
147
148
148
send ( message : string | ArrayBuffer | ArrayBufferView | Blob ) {
149
- if ( this . _readyState === WebSocketReadyState . CONNECTING ) {
149
+ if ( this . #readyState === WebSocketReadyState . CONNECTING ) {
150
150
throw new Error ( 'InvalidStateError' )
151
151
}
152
152
153
+ if ( this . #readyState !== WebSocketReadyState . OPEN ) {
154
+ return
155
+ }
156
+
153
157
if ( typeof message === 'string' ) {
154
158
this . ws . send ( message )
155
159
return
@@ -172,17 +176,15 @@ export class WebSocket
172
176
} ) ( )
173
177
return
174
178
}
175
-
176
- throw new TypeError ( 'Invalid message type' )
177
179
}
178
180
179
181
/**
180
182
* https://websockets.spec.whatwg.org/#dom-websocket-close
181
183
*/
182
184
close ( code : number = 1000 , reason : string = '' ) {
183
185
if (
184
- this . _readyState === WebSocketReadyState . CLOSING ||
185
- this . _readyState === WebSocketReadyState . CLOSED
186
+ this . #readyState === WebSocketReadyState . CLOSING ||
187
+ this . #readyState === WebSocketReadyState . CLOSED
186
188
) {
187
189
return
188
190
}
@@ -191,7 +193,7 @@ export class WebSocket
191
193
throw new Error ( 'Invalid close code. Must be 1000 or in range 3000-4999.' )
192
194
}
193
195
194
- this . _readyState = WebSocketReadyState . CLOSING
196
+ this . #readyState = WebSocketReadyState . CLOSING
195
197
this . ws . close ( code , reason )
196
198
}
197
199
0 commit comments