Skip to content

Commit

Permalink
Add ping example
Browse files Browse the repository at this point in the history
Closes #227
  • Loading branch information
nhooyr committed May 18, 2020
1 parent de8e29b commit 1695216
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions autobahn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ var autobahnCases = []string{"*"}
func TestAutobahn(t *testing.T) {
t.Parallel()

if os.Getenv("AUTOBAHN_TEST") == "" {
if os.Getenv("AUTOBAHN") == "" {
t.SkipNow()
}

if os.Getenv("AUTOBAHN_FAST") != "" {
if os.Getenv("AUTOBAHN") == "fast" {
// These are the slow tests.
excludedAutobahnCases = append(excludedAutobahnCases,
"9.*", "13.*", "12.*",
)
Expand Down
25 changes: 25 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,31 @@ func Example_crossOrigin() {
log.Fatal(err)
}

func ExampleConn_Ping() {
// Dials a server and pings it 5 times.

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")

// Required to read the Pongs from the server.
ctx = c.CloseRead(ctx)

for i := 0; i < 5; i++ {
err = c.Ping(ctx)
if err != nil {
log.Fatal(err)
}
}

c.Close(websocket.StatusNormalClosure, "")
}

// This example demonstrates how to create a WebSocket server
// that gracefully exits when sent a signal.
//
Expand Down

0 comments on commit 1695216

Please sign in to comment.