diff --git a/subscription.go b/subscription.go index 6c35e0d..fbfddc9 100644 --- a/subscription.go +++ b/subscription.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "net/http" "strings" "sync" "sync/atomic" @@ -87,6 +88,7 @@ type SubscriptionClient struct { url string conn WebsocketConn connectionParams map[string]interface{} + websocketOptions WebsocketOptions context context.Context subscriptions map[string]*subscription cancel context.CancelFunc @@ -138,6 +140,12 @@ func (sc *SubscriptionClient) WithWebSocket(fn func(sc *SubscriptionClient) (Web return sc } +// WithWebSocketOptions provides options to the websocket client +func (sc *SubscriptionClient) WithWebSocketOptions(options WebsocketOptions) *SubscriptionClient { + sc.websocketOptions = options + return sc +} + // WithConnectionParams updates connection params for sending to server through GQL_CONNECTION_INIT event // It's usually used for authentication handshake func (sc *SubscriptionClient) WithConnectionParams(params map[string]interface{}) *SubscriptionClient { @@ -602,7 +610,9 @@ func newWebsocketConn(sc *SubscriptionClient) (WebsocketConn, error) { options := &websocket.DialOptions{ Subprotocols: []string{"graphql-ws"}, + HTTPClient: sc.websocketOptions.HTTPClient, } + c, _, err := websocket.Dial(sc.GetContext(), sc.GetURL(), options) if err != nil { return nil, err @@ -614,3 +624,9 @@ func newWebsocketConn(sc *SubscriptionClient) (WebsocketConn, error) { timeout: sc.GetTimeout(), }, nil } + +// WebsocketOptions allows implementation agnostic configuration of the websocket client +type WebsocketOptions struct { + // HTTPClient is used for the connection. + HTTPClient *http.Client +}