(w http.ResponseWriter, r *http.Request)
| 21 | } |
| 22 | |
| 23 | func (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. |
nothing calls this directly
no test coverage detected