Skip to content

Commit 2679587

Browse files
authored
Merge pull request #7 from go-netty/feat-proxy
feat: Add WithDialer to support client proxy
2 parents 74930ba + baaf717 commit 2679587

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Option
3131
func WithBufferSize(readBufferSize, writeBufferSize int) Option
3232
func WithCompress(compressLevel int, compressThreshold int64) Option
3333
func WithClientHeader(header http.Header) Option
34+
func WithDialer(dialer Dialer) Option
3435
func WithMaxFrameSize(maxFrameSize int64) Option
3536
func WithNoDelay(noDelay bool) Option
3637
func WithServerHeader(header http.Header) Option
@@ -125,13 +126,10 @@ ws.OnClose = func(conn nettyws.Conn, err error) {
125126
fmt.Println("upgrade websocket connections ....")
126127

127128
// upgrade websocket connection from http server
128-
serveMux := http.NewServeMux()
129-
serveMux.HandleFunc("/ws", func(writer http.ResponseWriter, request *http.Request) {
130-
ws.UpgradeHTTP(writer, request)
131-
})
129+
http.Handle("/ws", ws)
132130

133131
// listen http server
134-
if err := http.ListenAndServe(":9527", serveMux); nil != err {
132+
if err := http.ListenAndServe(":9527", nil); nil != err {
135133
panic(err)
136134
}
137135
```

example/http-server.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,10 @@ func main() {
4747
fmt.Println("upgrade websocket connections ....")
4848

4949
// upgrade websocket connection from http server
50-
serveMux := http.NewServeMux()
51-
serveMux.HandleFunc("/ws", func(writer http.ResponseWriter, request *http.Request) {
52-
ws.UpgradeHTTP(writer, request)
53-
})
50+
http.Handle("/ws", ws)
5451

5552
// listen http server
56-
if err := http.ListenAndServe(":9527", serveMux); nil != err {
53+
if err := http.ListenAndServe(":9527", nil); nil != err {
5754
panic(err)
5855
}
5956
}

nettyws.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ func (ws *Websocket) Close() error {
102102
return nil
103103
}
104104

105+
func (ws *Websocket) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
106+
select {
107+
case <-ws.ctx.Done():
108+
http.Error(writer, "http: server shutdown", http.StatusNotAcceptable)
109+
default:
110+
_, _ = ws.upgrader.Upgrade(writer, request)
111+
}
112+
}
113+
105114
// UpgradeHTTP upgrades http connection to the websocket connection
106115
func (ws *Websocket) UpgradeHTTP(writer http.ResponseWriter, request *http.Request) (conn Conn, err error) {
107116

options.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package nettyws
1818

1919
import (
20+
"context"
2021
"crypto/tls"
22+
"net"
2123
"net/http"
2224

2325
"github.com/go-netty/go-netty"
@@ -35,6 +37,17 @@ const (
3537
MsgBinary
3638
)
3739

40+
// Dialer is a means to establish a connection.
41+
type Dialer interface {
42+
// Dial connects to the given address via the proxy.
43+
Dial(network, addr string) (c net.Conn, err error)
44+
}
45+
46+
// contextDialer dials using a context
47+
type contextDialer interface {
48+
DialContext(ctx context.Context, network, address string) (net.Conn, error)
49+
}
50+
3851
type options struct {
3952
engine netty.Bootstrap
4053
serveMux *http.ServeMux
@@ -50,6 +63,7 @@ type options struct {
5063
compressThreshold int64
5164
requestHeader http.Header
5265
responseHeader http.Header
66+
dialer Dialer
5367
}
5468

5569
func parseOptions(opt ...Option) *options {
@@ -78,6 +92,16 @@ func (wso *options) wsOptions() *websocket.Options {
7892
dialer.Header = ws.HandshakeHeaderHTTP(wso.requestHeader)
7993
}
8094

95+
if wso.dialer != nil {
96+
ctxDialer, isCtxDialer := wso.dialer.(contextDialer)
97+
dialer.NetDial = func(ctx context.Context, network, addr string) (net.Conn, error) {
98+
if isCtxDialer {
99+
return ctxDialer.DialContext(ctx, network, addr)
100+
}
101+
return wso.dialer.Dial(network, addr)
102+
}
103+
}
104+
81105
var upgrader = ws.DefaultHTTPUpgrader
82106
if wso.responseHeader != nil {
83107
upgrader.Header = wso.responseHeader
@@ -192,3 +216,10 @@ func WithServerHeader(header http.Header) Option {
192216
options.responseHeader = header
193217
}
194218
}
219+
220+
// WithDialer specify the client to connect to the network via a dialer.
221+
func WithDialer(dialer Dialer) Option {
222+
return func(options *options) {
223+
options.dialer = dialer
224+
}
225+
}

0 commit comments

Comments
 (0)