example_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. var (
  12. c *websocket.Conn
  13. req *http.Request
  14. )
  15. // The websocket.IsUnexpectedCloseError function is useful for identifying
  16. // application and protocol errors.
  17. //
  18. // This server application works with a client application running in the
  19. // browser. The client application does not explicitly close the websocket. The
  20. // only expected close message from the client has the code
  21. // websocket.CloseGoingAway. All other other close messages are likely the
  22. // result of an application or protocol error and are logged to aid debugging.
  23. func ExampleIsUnexpectedCloseError() {
  24. for {
  25. messageType, p, err := c.ReadMessage()
  26. if err != nil {
  27. if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
  28. log.Printf("error: %v, user-agent: %v", err, req.Header.Get("User-Agent"))
  29. }
  30. return
  31. }
  32. processMesage(messageType, p)
  33. }
  34. }
  35. func processMesage(mt int, p []byte) {}
  36. // TestX prevents godoc from showing this entire file in the example. Remove
  37. // this function when a second example is added.
  38. func TestX(t *testing.T) {}