Skip to content

Commit 838e6bd

Browse files
authored
support custom http client for subscription client (#1)
1 parent dedae85 commit 838e6bd

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

subscription.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8+
"net/http"
89
"strings"
910
"sync"
1011
"sync/atomic"
@@ -87,6 +88,7 @@ type SubscriptionClient struct {
8788
url string
8889
conn WebsocketConn
8990
connectionParams map[string]interface{}
91+
websocketOptions WebsocketOptions
9092
context context.Context
9193
subscriptions map[string]*subscription
9294
cancel context.CancelFunc
@@ -138,6 +140,12 @@ func (sc *SubscriptionClient) WithWebSocket(fn func(sc *SubscriptionClient) (Web
138140
return sc
139141
}
140142

143+
// WithWebSocketOptions provides options to the websocket client
144+
func (sc *SubscriptionClient) WithWebSocketOptions(options WebsocketOptions) *SubscriptionClient {
145+
sc.websocketOptions = options
146+
return sc
147+
}
148+
141149
// WithConnectionParams updates connection params for sending to server through GQL_CONNECTION_INIT event
142150
// It's usually used for authentication handshake
143151
func (sc *SubscriptionClient) WithConnectionParams(params map[string]interface{}) *SubscriptionClient {
@@ -602,7 +610,9 @@ func newWebsocketConn(sc *SubscriptionClient) (WebsocketConn, error) {
602610

603611
options := &websocket.DialOptions{
604612
Subprotocols: []string{"graphql-ws"},
613+
HTTPClient: sc.websocketOptions.HTTPClient,
605614
}
615+
606616
c, _, err := websocket.Dial(sc.GetContext(), sc.GetURL(), options)
607617
if err != nil {
608618
return nil, err
@@ -614,3 +624,9 @@ func newWebsocketConn(sc *SubscriptionClient) (WebsocketConn, error) {
614624
timeout: sc.GetTimeout(),
615625
}, nil
616626
}
627+
628+
// WebsocketOptions allows implementation agnostic configuration of the websocket client
629+
type WebsocketOptions struct {
630+
// HTTPClient is used for the connection.
631+
HTTPClient *http.Client
632+
}

0 commit comments

Comments
 (0)