example_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2015 The Gorilla WebSocket Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package websocket_test
  5. import (
  6. "log"
  7. "net/http"
  8. "testing"
  9. "github.com/gorilla/websocket"
  10. )
  11. // The websocket.IsUnexpectedCloseError function is useful for identifying
  12. // application and protocol errors.
  13. //
  14. // This server application works with a client application running in the
  15. // browser. The client application does not explicitly close the websocket. The
  16. // only expected close message from the client has the code
  17. // websocket.CloseGoingAway. All other other close messages are likely the
  18. // result of an application or protocol error and are logged to aid debugging.
  19. func ExampleIsUnexpectedCloseError(err error, c *websocket.Conn, req *http.Request) {
  20. for {
  21. messageType, p, err := c.ReadMessage()
  22. if err != nil {
  23. if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
  24. log.Println("error: %v, user-agent: %v", err, req.Header.Get("User-Agent"))
  25. }
  26. return
  27. }
  28. processMesage(messageType, p)
  29. }
  30. }
  31. func processMesage(mt int, p []byte) {}
  32. // TestX prevents godoc from showing this entire file in the example. Remove
  33. // this function when a second example is added.
  34. func TestX(t *testing.T) {}