2
0

Add support for configuring Proxy for recws, and a go.mod mod definition file

This commit is contained in:
Tim Darby
2019-05-24 17:56:31 +01:00
parent 3a47c98d71
commit 46c8e11f64
3 changed files with 21 additions and 1 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ package main
import (
"context"
"github.com/mariuspass/recws"
"github.com/timmersuk/recws"
"log"
"time"
)
+6
View File
@@ -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
)
+14
View File
@@ -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