Skip to content

Commit f2ecfe0

Browse files
committed
websocket: fix handler name and defaulthandler
1 parent a781096 commit f2ecfe0

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

websocketproxy.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@ import (
1111
)
1212

1313
var (
14-
// DefaultUpgrader specifies the paramaters for upgrading an HTTP connection to
15-
// a WebSocket connection.
14+
// DefaultUpgrader specifies the paramaters for upgrading an HTTP
15+
// connection to a WebSocket connection.
1616
DefaultUpgrader = &websocket.Upgrader{
17-
ReadBufferSize: 4096,
18-
WriteBufferSize: 4096,
19-
CheckOrigin: func(r *http.Request) bool {
20-
return true
21-
},
17+
ReadBufferSize: 1024,
18+
WriteBufferSize: 1024,
2219
}
2320

2421
// DefaultDialer is a dialer with all fields set to the default zero values.
@@ -45,7 +42,7 @@ type WebsocketProxy struct {
4542
// request to the given target.
4643
func ProxyHandler(target *url.URL) http.Handler {
4744
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
48-
NewProxy(target).ServerHTTP(rw, req)
45+
NewProxy(target).ServeHTTP(rw, req)
4946
})
5047
}
5148

@@ -56,8 +53,8 @@ func NewProxy(target *url.URL) *WebsocketProxy {
5653
return &WebsocketProxy{Backend: backend}
5754
}
5855

59-
// ServerHTTP implements the http.Handler that proxies WebSocket connections.
60-
func (w *WebsocketProxy) ServerHTTP(rw http.ResponseWriter, req *http.Request) {
56+
// ServeHTTP implements the http.Handler that proxies WebSocket connections.
57+
func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
6158
upgrader := w.Upgrader
6259
if w.Upgrader == nil {
6360
upgrader = DefaultUpgrader
@@ -92,5 +89,6 @@ func (w *WebsocketProxy) ServerHTTP(rw http.ResponseWriter, req *http.Request) {
9289

9390
go cp(connBackend.UnderlyingConn(), connPub.UnderlyingConn())
9491
go cp(connPub.UnderlyingConn(), connBackend.UnderlyingConn())
95-
<-errc
92+
err = <-errc
93+
log.Println("websocketproxy: connection ended %s", err)
9694
}

websocketproxy_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,22 @@ var (
1515
backendURL = "ws://127.0.0.1:8888"
1616
)
1717

18-
func ProxyFunc(w http.ResponseWriter, r *http.Request) {
19-
u, _ := url.Parse("ws://127.0.0.1:8888")
20-
ProxyHandler(u).ServeHTTP(w, r)
21-
}
22-
2318
func TestProxy(t *testing.T) {
2419
// websocket proxy
20+
upgrader := &websocket.Upgrader{
21+
ReadBufferSize: 4096,
22+
WriteBufferSize: 4096,
23+
CheckOrigin: func(r *http.Request) bool {
24+
return true
25+
},
26+
}
27+
28+
u, _ := url.Parse(backendURL)
29+
proxy := NewProxy(u)
30+
proxy.Upgrader = upgrader
31+
2532
mux := http.NewServeMux()
26-
mux.HandleFunc("/proxy", ProxyFunc)
33+
mux.Handle("/proxy", proxy)
2734
go func() {
2835
if err := http.ListenAndServe(":7777", mux); err != nil {
2936
t.Fatal("ListenAndServe: ", err)
@@ -36,7 +43,7 @@ func TestProxy(t *testing.T) {
3643
go func() {
3744
mux2 := http.NewServeMux()
3845
mux2.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
39-
conn, err := DefaultUpgrader.Upgrade(w, r, nil)
46+
conn, err := upgrader.Upgrade(w, r, nil)
4047
if err != nil {
4148
log.Println(err)
4249
return

0 commit comments

Comments
 (0)