good morning!!!!

Skip to content
Snippets Groups Projects
Unverified Commit cdeb9806 authored by Anmol Sethi's avatar Anmol Sethi
Browse files

ws_js.go: Add CloseNow

parent 25a5ca47
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
// //
// - Accept always errors out // - Accept always errors out
// - Conn.Ping is no-op // - Conn.Ping is no-op
// - Conn.CloseNow is Close(StatusGoingAway, "")
// - HTTPClient, HTTPHeader and CompressionMode in DialOptions are no-op // - HTTPClient, HTTPHeader and CompressionMode in DialOptions are no-op
// - *http.Response from Dial is &http.Response{} with a 101 status code on success // - *http.Response from Dial is &http.Response{} with a 101 status code on success
package websocket // import "nhooyr.io/websocket" package websocket // import "nhooyr.io/websocket"
...@@ -151,7 +151,7 @@ func (c *Conn) read(ctx context.Context) (MessageType, []byte, error) { ...@@ -151,7 +151,7 @@ func (c *Conn) read(ctx context.Context) (MessageType, []byte, error) {
return 0, nil, ctx.Err() return 0, nil, ctx.Err()
case <-c.readSignal: case <-c.readSignal:
case <-c.closed: case <-c.closed:
return 0, nil, c.closeErr return 0, nil, errClosed
} }
c.readBufMu.Lock() c.readBufMu.Lock()
...@@ -205,7 +205,7 @@ func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error { ...@@ -205,7 +205,7 @@ func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error {
func (c *Conn) write(ctx context.Context, typ MessageType, p []byte) error { func (c *Conn) write(ctx context.Context, typ MessageType, p []byte) error {
if c.isClosed() { if c.isClosed() {
return c.closeErr return errClosed
} }
switch typ { switch typ {
case MessageBinary: case MessageBinary:
...@@ -229,19 +229,28 @@ func (c *Conn) Close(code StatusCode, reason string) error { ...@@ -229,19 +229,28 @@ func (c *Conn) Close(code StatusCode, reason string) error {
return nil return nil
} }
// CloseNow closes the WebSocket connection without attempting a close handshake.
// Use When you do not want the overhead of the close handshake.
//
// note: No different from Close(StatusGoingAway, "") in WASM as there is no way to close
// a WebSocket without the close handshake.
func (c *Conn) CloseNow() error {
return c.Close(StatusGoingAway, "")
}
func (c *Conn) exportedClose(code StatusCode, reason string) error { func (c *Conn) exportedClose(code StatusCode, reason string) error {
c.closingMu.Lock() c.closingMu.Lock()
defer c.closingMu.Unlock() defer c.closingMu.Unlock()
if c.isClosed() {
return errClosed
}
ce := fmt.Errorf("sent close: %w", CloseError{ ce := fmt.Errorf("sent close: %w", CloseError{
Code: code, Code: code,
Reason: reason, Reason: reason,
}) })
if c.isClosed() {
return fmt.Errorf("tried to close with %q but connection already closed: %w", ce, c.closeErr)
}
c.setCloseErr(ce) c.setCloseErr(ce)
err := c.ws.Close(int(code), reason) err := c.ws.Close(int(code), reason)
if err != nil { if err != nil {
...@@ -312,7 +321,7 @@ func dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *http.Resp ...@@ -312,7 +321,7 @@ func dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *http.Resp
StatusCode: http.StatusSwitchingProtocols, StatusCode: http.StatusSwitchingProtocols,
}, nil }, nil
case <-c.closed: case <-c.closed:
return nil, nil, c.closeErr return nil, nil, errClosed
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment