2016-01-20 00:06:29 +03:00
|
|
|
// Copyright 2015 The Gorilla WebSocket Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package websocket_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
2016-03-03 01:56:29 +03:00
|
|
|
var (
|
|
|
|
c *websocket.Conn
|
|
|
|
req *http.Request
|
|
|
|
)
|
|
|
|
|
2016-01-20 00:06:29 +03:00
|
|
|
// The websocket.IsUnexpectedCloseError function is useful for identifying
|
|
|
|
// application and protocol errors.
|
|
|
|
//
|
|
|
|
// This server application works with a client application running in the
|
|
|
|
// browser. The client application does not explicitly close the websocket. The
|
|
|
|
// only expected close message from the client has the code
|
2020-03-19 16:49:51 +03:00
|
|
|
// websocket.CloseGoingAway. All other close messages are likely the
|
2016-01-20 00:06:29 +03:00
|
|
|
// result of an application or protocol error and are logged to aid debugging.
|
2016-03-03 01:56:29 +03:00
|
|
|
func ExampleIsUnexpectedCloseError() {
|
2016-01-20 00:06:29 +03:00
|
|
|
for {
|
|
|
|
messageType, p, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
|
2016-01-20 05:24:46 +03:00
|
|
|
log.Printf("error: %v, user-agent: %v", err, req.Header.Get("User-Agent"))
|
2016-01-20 00:06:29 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-03-19 16:49:51 +03:00
|
|
|
processMessage(messageType, p)
|
2016-01-20 00:06:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:49:51 +03:00
|
|
|
func processMessage(mt int, p []byte) {}
|
2016-01-20 00:06:29 +03:00
|
|
|
|
|
|
|
// TestX prevents godoc from showing this entire file in the example. Remove
|
|
|
|
// this function when a second example is added.
|
2024-06-14 05:53:52 +03:00
|
|
|
func TestX(t *testing.T) {}
|