| 11 | ) |
| 12 | |
| 13 | func ExampleAccept() { |
| 14 | // This handler accepts a WebSocket connection, reads a single JSON |
| 15 | // message from the client and then closes the connection. |
| 16 | |
| 17 | fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | c, err := websocket.Accept(w, r, nil) |
| 19 | if err != nil { |
| 20 | log.Println(err) |
| 21 | return |
| 22 | } |
| 23 | defer c.CloseNow() |
| 24 | |
| 25 | ctx, cancel := context.WithTimeout(r.Context(), time.Second*10) |
| 26 | defer cancel() |
| 27 | |
| 28 | var v any |
| 29 | err = wsjson.Read(ctx, c, &v) |
| 30 | if err != nil { |
| 31 | log.Println(err) |
| 32 | return |
| 33 | } |
| 34 | |
| 35 | c.Close(websocket.StatusNormalClosure, "") |
| 36 | }) |
| 37 | |
| 38 | err := http.ListenAndServe("localhost:8080", fn) |
| 39 | log.Fatal(err) |
| 40 | } |
| 41 | |
| 42 | func ExampleDial() { |
| 43 | // Dials a server, writes a single JSON message and then |