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
Loading
......@@ -28,6 +28,7 @@
//
// - Accept always errors out
// - Conn.Ping is no-op
// - Conn.CloseNow is Close(StatusGoingAway, "")
// - HTTPClient, HTTPHeader and CompressionMode in DialOptions are no-op
// - *http.Response from Dial is &http.Response{} with a 101 status code on success
package websocket // import "nhooyr.io/websocket"
......@@ -151,7 +151,7 @@ func (c *Conn) read(ctx context.Context) (MessageType, []byte, error) {
return 0, nil, ctx.Err()
case <-c.readSignal:
case <-c.closed:
return 0, nil, c.closeErr
return 0, nil, errClosed
}
c.readBufMu.Lock()
......@@ -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 {
if c.isClosed() {
return c.closeErr
return errClosed
}
switch typ {
case MessageBinary:
......@@ -229,19 +229,28 @@ func (c *Conn) Close(code StatusCode, reason string) error {
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 {
c.closingMu.Lock()
defer c.closingMu.Unlock()
if c.isClosed() {
return errClosed
}
ce := fmt.Errorf("sent close: %w", CloseError{
Code: code,
Reason: reason,
})
if c.isClosed() {
return fmt.Errorf("tried to close with %q but connection already closed: %w", ce, c.closeErr)
}
c.setCloseErr(ce)
err := c.ws.Close(int(code), reason)
if err != nil {
......@@ -312,7 +321,7 @@ func dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *http.Resp
StatusCode: http.StatusSwitchingProtocols,
}, nil
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.
Finish editing this message first!
Please register or to comment