OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / gorilla / websocket / example_test.go
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 other close messages are likely the
27 // result of an application or protocol error and are logged to aid debugging.
28 func ExampleIsUnexpectedCloseError() {
29
30         for {
31                 messageType, p, err := c.ReadMessage()
32                 if err != nil {
33                         if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
34                                 log.Printf("error: %v, user-agent: %v", err, req.Header.Get("User-Agent"))
35                         }
36                         return
37                 }
38                 processMesage(messageType, p)
39         }
40 }
41
42 func processMesage(mt int, p []byte) {}
43
44 // TestX prevents godoc from showing this entire file in the example. Remove
45 // this function when a second example is added.
46 func TestX(t *testing.T) {}