MCPcopy
hub / github.com/coder/websocket / ExampleAccept

Function ExampleAccept

example_test.go:13–40  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

11)
12
13func 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
42func ExampleDial() {
43 // Dials a server, writes a single JSON message and then

Callers

nothing calls this directly

Calls 4

AcceptFunction · 0.92
ReadFunction · 0.92
CloseNowMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…