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

Method ServeHTTP

internal/examples/echo/server.go:23–49  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

21}
22
23func (s echoServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
24 c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
25 Subprotocols: []string{"echo"},
26 })
27 if err != nil {
28 s.logf("%v", err)
29 return
30 }
31 defer c.CloseNow()
32
33 if c.Subprotocol() != "echo" {
34 c.Close(websocket.StatusPolicyViolation, "client must speak the echo subprotocol")
35 return
36 }
37
38 l := rate.NewLimiter(rate.Every(time.Millisecond*100), 10)
39 for {
40 err = echo(c, l)
41 if websocket.CloseStatus(err) == websocket.StatusNormalClosure {
42 return
43 }
44 if err != nil {
45 s.logf("failed to echo with %v: %v", r.RemoteAddr, err)
46 return
47 }
48 }
49}
50
51// echo reads from the WebSocket connection and then writes
52// the received message back to it.

Callers

nothing calls this directly

Calls 6

AcceptFunction · 0.92
CloseStatusFunction · 0.92
echoFunction · 0.85
CloseNowMethod · 0.45
SubprotocolMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected