Skip to content

Commit 2eeb8dd

Browse files
cpaciafjl
authored andcommitted
rpc: add DialWebsocketWithDialer (#20471)
This commit intents to replicate the DialHTTPWithClient function which allows creating a RPC Client using a custom dialer but for websockets. We introduce a new DialWebsocketWithDialer function which allows the caller to instantiate a new websocket client using a custom dialer.
1 parent b7cf41e commit 2eeb8dd

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

rpc/websocket.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,13 @@ func (e wsHandshakeError) Error() string {
124124
return s
125125
}
126126

127-
// DialWebsocket creates a new RPC client that communicates with a JSON-RPC server
128-
// that is listening on the given endpoint.
129-
//
130-
// The context is used for the initial connection establishment. It does not
131-
// affect subsequent interactions with the client.
132-
func DialWebsocket(ctx context.Context, endpoint, origin string) (*Client, error) {
127+
// DialWebsocketWithDialer creates a new RPC client that communicates with a JSON-RPC server
128+
// that is listening on the given endpoint using the provided dialer.
129+
func DialWebsocketWithDialer(ctx context.Context, endpoint, origin string, dialer websocket.Dialer) (*Client, error) {
133130
endpoint, header, err := wsClientHeaders(endpoint, origin)
134131
if err != nil {
135132
return nil, err
136133
}
137-
dialer := websocket.Dialer{
138-
ReadBufferSize: wsReadBuffer,
139-
WriteBufferSize: wsWriteBuffer,
140-
WriteBufferPool: wsBufferPool,
141-
}
142134
return newClient(ctx, func(ctx context.Context) (ServerCodec, error) {
143135
conn, resp, err := dialer.DialContext(ctx, endpoint, header)
144136
if err != nil {
@@ -152,6 +144,20 @@ func DialWebsocket(ctx context.Context, endpoint, origin string) (*Client, error
152144
})
153145
}
154146

147+
// DialWebsocket creates a new RPC client that communicates with a JSON-RPC server
148+
// that is listening on the given endpoint.
149+
//
150+
// The context is used for the initial connection establishment. It does not
151+
// affect subsequent interactions with the client.
152+
func DialWebsocket(ctx context.Context, endpoint, origin string) (*Client, error) {
153+
dialer := websocket.Dialer{
154+
ReadBufferSize: wsReadBuffer,
155+
WriteBufferSize: wsWriteBuffer,
156+
WriteBufferPool: wsBufferPool,
157+
}
158+
return DialWebsocketWithDialer(ctx, endpoint, origin, dialer)
159+
}
160+
155161
func wsClientHeaders(endpoint, origin string) (string, http.Header, error) {
156162
endpointURL, err := url.Parse(endpoint)
157163
if err != nil {

0 commit comments

Comments
 (0)