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.
This commit is contained in:
parent
b7cf41e4b3
commit
2eeb8dd271
@ -124,21 +124,13 @@ func (e wsHandshakeError) Error() string {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialWebsocket creates a new RPC client that communicates with a JSON-RPC server
|
// DialWebsocketWithDialer creates a new RPC client that communicates with a JSON-RPC server
|
||||||
// that is listening on the given endpoint.
|
// that is listening on the given endpoint using the provided dialer.
|
||||||
//
|
func DialWebsocketWithDialer(ctx context.Context, endpoint, origin string, dialer websocket.Dialer) (*Client, error) {
|
||||||
// The context is used for the initial connection establishment. It does not
|
|
||||||
// affect subsequent interactions with the client.
|
|
||||||
func DialWebsocket(ctx context.Context, endpoint, origin string) (*Client, error) {
|
|
||||||
endpoint, header, err := wsClientHeaders(endpoint, origin)
|
endpoint, header, err := wsClientHeaders(endpoint, origin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
dialer := websocket.Dialer{
|
|
||||||
ReadBufferSize: wsReadBuffer,
|
|
||||||
WriteBufferSize: wsWriteBuffer,
|
|
||||||
WriteBufferPool: wsBufferPool,
|
|
||||||
}
|
|
||||||
return newClient(ctx, func(ctx context.Context) (ServerCodec, error) {
|
return newClient(ctx, func(ctx context.Context) (ServerCodec, error) {
|
||||||
conn, resp, err := dialer.DialContext(ctx, endpoint, header)
|
conn, resp, err := dialer.DialContext(ctx, endpoint, header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -152,6 +144,20 @@ func DialWebsocket(ctx context.Context, endpoint, origin string) (*Client, error
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DialWebsocket creates a new RPC client that communicates with a JSON-RPC server
|
||||||
|
// that is listening on the given endpoint.
|
||||||
|
//
|
||||||
|
// The context is used for the initial connection establishment. It does not
|
||||||
|
// affect subsequent interactions with the client.
|
||||||
|
func DialWebsocket(ctx context.Context, endpoint, origin string) (*Client, error) {
|
||||||
|
dialer := websocket.Dialer{
|
||||||
|
ReadBufferSize: wsReadBuffer,
|
||||||
|
WriteBufferSize: wsWriteBuffer,
|
||||||
|
WriteBufferPool: wsBufferPool,
|
||||||
|
}
|
||||||
|
return DialWebsocketWithDialer(ctx, endpoint, origin, dialer)
|
||||||
|
}
|
||||||
|
|
||||||
func wsClientHeaders(endpoint, origin string) (string, http.Header, error) {
|
func wsClientHeaders(endpoint, origin string) (string, http.Header, error) {
|
||||||
endpointURL, err := url.Parse(endpoint)
|
endpointURL, err := url.Parse(endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user