diff --git a/examples/basic.go b/examples/basic.go index ad6b5a7..21f1453 100644 --- a/examples/basic.go +++ b/examples/basic.go @@ -2,7 +2,7 @@ package main import ( "context" - "github.com/mariuspass/recws" + "github.com/timmersuk/recws" "log" "time" ) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a85cc5b --- /dev/null +++ b/go.mod @@ -0,0 +1,6 @@ +module github.com/mariuspass/recws + +require ( + github.com/gorilla/websocket v1.4.0 + github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7 +) diff --git a/recws.go b/recws.go index da1f9fe..5fbfb5f 100644 --- a/recws.go +++ b/recws.go @@ -33,6 +33,9 @@ type RecConn struct { // HandshakeTimeout specifies the duration for the handshake to complete, // default to 2 seconds HandshakeTimeout time.Duration + // Proxy specifies the proxy function for the dialer + // defaults to ProxyFromEnvironment + Proxy func(*http.Request) (*url.URL, error) // NonVerbose suppress connecting/reconnecting messages. NonVerbose bool // SubscribeHandler fires after the connection successfully establish. @@ -231,12 +234,22 @@ func (rc *RecConn) setDefaultHandshakeTimeout() { } } +func (rc *RecConn) setDefaultProxy() { + rc.mu.Lock() + defer rc.mu.Unlock() + + if rc.Proxy == nil { + rc.Proxy = http.ProxyFromEnvironment + } +} + func (rc *RecConn) setDefaultDialer(handshakeTimeout time.Duration) { rc.mu.Lock() defer rc.mu.Unlock() rc.dialer = &websocket.Dialer{ HandshakeTimeout: handshakeTimeout, + Proxy: rc.Proxy, } } @@ -266,6 +279,7 @@ func (rc *RecConn) Dial(urlStr string, reqHeader http.Header) { rc.setDefaultRecIntvlMax() rc.setDefaultRecIntvlFactor() rc.setDefaultHandshakeTimeout() + rc.setDefaultProxy() rc.setDefaultDialer(rc.getHandshakeTimeout()) // Connect