good morning!!!!

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

Add a CloseError example

Closes #128
parent 1d635994
No related branches found
No related tags found
No related merge requests found
......@@ -32,6 +32,8 @@ go get nhooyr.io/websocket@v1.5.0
For a production quality example that shows off the full API, see the [echo example on the godoc](https://godoc.org/nhooyr.io/websocket#example-package--Echo). On github, the example is at [example_echo_test.go](./example_echo_test.go).
Please use the [golang.org/x/xerrors](https://godoc.org/golang.org/x/xerrors#As) package to check for [websocket.CloseError](https://godoc.org/nhooyr.io/websocket#CloseError). See the [CloseError godoc example](https://godoc.org/nhooyr.io/websocket#example-CloseError).
### Server
```go
......
......@@ -6,6 +6,8 @@ import (
"net/http"
"time"
"golang.org/x/xerrors"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)
......@@ -60,6 +62,26 @@ func ExampleDial() {
c.Close(websocket.StatusNormalClosure, "")
}
// This example dials a server and then expects to be disconnected with status code
// websocket.StatusNormalClosure.
func ExampleCloseError() {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)
if err != nil {
log.Fatal(err)
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
_, _, err = c.Reader(ctx)
var cerr websocket.CloseError
if !xerrors.As(err, &cerr) || cerr.Code != websocket.StatusNormalClosure {
log.Fatalf("expected to be disconnected with StatusNormalClosure but got: %+v", err)
return
}
}
// This example shows how to correctly handle a WebSocket connection
// on which you will only write and do not expect to read data messages.
func Example_writeOnly() {
......
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