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