@@ -151,7 +151,7 @@ func (c *Conn) read(ctx context.Context) (MessageType, []byte, error) {
151
151
return 0 , nil , ctx .Err ()
152
152
case <- c .readSignal :
153
153
case <- c .closed :
154
- return 0 , nil , c . closeErr
154
+ return 0 , nil , errClosed
155
155
}
156
156
157
157
c .readBufMu .Lock ()
@@ -205,7 +205,7 @@ func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error {
205
205
206
206
func (c * Conn ) write (ctx context.Context , typ MessageType , p []byte ) error {
207
207
if c .isClosed () {
208
- return c . closeErr
208
+ return errClosed
209
209
}
210
210
switch typ {
211
211
case MessageBinary :
@@ -229,19 +229,28 @@ func (c *Conn) Close(code StatusCode, reason string) error {
229
229
return nil
230
230
}
231
231
232
+ // CloseNow closes the WebSocket connection without attempting a close handshake.
233
+ // Use When you do not want the overhead of the close handshake.
234
+ //
235
+ // note: No different from Close(StatusGoingAway, "") in WASM as there is no way to close
236
+ // a WebSocket without the close handshake.
237
+ func (c * Conn ) CloseNow () error {
238
+ return c .Close (StatusGoingAway , "" )
239
+ }
240
+
232
241
func (c * Conn ) exportedClose (code StatusCode , reason string ) error {
233
242
c .closingMu .Lock ()
234
243
defer c .closingMu .Unlock ()
235
244
245
+ if c .isClosed () {
246
+ return errClosed
247
+ }
248
+
236
249
ce := fmt .Errorf ("sent close: %w" , CloseError {
237
250
Code : code ,
238
251
Reason : reason ,
239
252
})
240
253
241
- if c .isClosed () {
242
- return fmt .Errorf ("tried to close with %q but connection already closed: %w" , ce , c .closeErr )
243
- }
244
-
245
254
c .setCloseErr (ce )
246
255
err := c .ws .Close (int (code ), reason )
247
256
if err != nil {
@@ -312,7 +321,7 @@ func dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *http.Resp
312
321
StatusCode : http .StatusSwitchingProtocols ,
313
322
}, nil
314
323
case <- c .closed :
315
- return nil , nil , c . closeErr
324
+ return nil , nil , errClosed
316
325
}
317
326
}
318
327
0 commit comments