vendor/github.com/go-rod/rod/lib/cdp/utils.go
46 lines · 1143 bytes
1package cdp
2
3import (
4 "context"
5 "crypto/tls"
6 "net"
7 "net/http"
8
9 "github.com/go-rod/rod/lib/utils"
10)
11
12// Dialer interface for WebSocket connection.
13type Dialer interface {
14 DialContext(ctx context.Context, network, address string) (net.Conn, error)
15}
16
17// TODO: replace it with tls.Dialer once golang v1.15 is widely used.
18type tlsDialer struct{}
19
20func (d *tlsDialer) DialContext(_ context.Context, network, address string) (net.Conn, error) {
21 return tls.Dial(network, address, nil)
22}
23
24// MustConnectWS helper to make a websocket connection.
25func MustConnectWS(wsURL string) WebSocketable {
26 ws := &WebSocket{}
27 utils.E(ws.Connect(context.Background(), wsURL, nil))
28 return ws
29}
30
31// MustStartWithURL helper for ConnectURL.
32func MustStartWithURL(ctx context.Context, u string, h http.Header) *Client {
33 c, err := StartWithURL(ctx, u, h)
34 utils.E(err)
35 return c
36}
37
38// StartWithURL helper to connect to the u with the default websocket lib.
39func StartWithURL(ctx context.Context, u string, h http.Header) (*Client, error) {
40 ws := &WebSocket{}
41 err := ws.Connect(ctx, u, h)
42 if err != nil {
43 return nil, err
44 }
45 return New().Start(ws), nil
46}