Adding client API changes prior to package rewrite

This commit is contained in:
Kenneth Shaw 2018-07-13 11:24:37 +07:00
parent d413f67302
commit 4ae10864e4
4 changed files with 14 additions and 9 deletions

View File

@ -4,7 +4,7 @@ import "fmt"
//go:generate easyjson -omit_empty -output_filename easyjson.go chrome.go
// Chrome holds connection information for a Chrome, Edge, or Safari target.
// Chrome holds connection information for a Chrome target.
//
//easyjson:json
type Chrome struct {
@ -20,7 +20,7 @@ type Chrome struct {
// String satisfies the stringer interface.
func (c Chrome) String() string {
return fmt.Sprintf("%s (`%s`)", c.ID, c.Title)
return fmt.Sprintf("[%s]: %q", c.ID, c.Title)
}
// GetID returns the target ID.
@ -33,6 +33,12 @@ func (c *Chrome) GetType() TargetType {
return c.Type
}
// GetDevtoolsURL returns the devtools frontend target URL, satisfying the
// domains.Target interface.
func (c *Chrome) GetDevtoolsURL() string {
return c.DevtoolsURL
}
// GetWebsocketURL provides the websocket URL for the target, satisfying the
// domains.Target interface.
func (c *Chrome) GetWebsocketURL() string {

View File

@ -50,6 +50,7 @@ type Target interface {
String() string
GetID() string
GetType() TargetType
GetDevtoolsURL() string
GetWebsocketURL() string
}

View File

@ -6,7 +6,7 @@ import (
"github.com/gorilla/websocket"
)
const (
var (
// DefaultReadBufferSize is the default maximum read buffer size.
DefaultReadBufferSize = 25 * 1024 * 1024
@ -14,7 +14,7 @@ const (
DefaultWriteBufferSize = 10 * 1024 * 1024
)
// Transport is the common interface to send/receive messages.
// Transport is the common interface to send/receive messages to a target.
type Transport interface {
Read() ([]byte, error)
Write([]byte) error
@ -43,7 +43,7 @@ func (c *Conn) Write(buf []byte) error {
// Dial dials the specified target's websocket URL.
//
// Note: uses gorilla/websocket.
func Dial(t Target, opts ...DialOption) (Transport, error) {
func Dial(urlstr string, opts ...DialOption) (Transport, error) {
d := &websocket.Dialer{
ReadBufferSize: DefaultReadBufferSize,
WriteBufferSize: DefaultWriteBufferSize,
@ -55,7 +55,7 @@ func Dial(t Target, opts ...DialOption) (Transport, error) {
}
// connect
conn, _, err := d.Dial(t.GetWebsocketURL(), nil)
conn, _, err := d.Dial(urlstr, nil)
if err != nil {
return nil, err
}
@ -65,5 +65,3 @@ func Dial(t Target, opts ...DialOption) (Transport, error) {
// DialOption is a dial option.
type DialOption func(*websocket.Dialer)
// TODO: add dial options ...

View File

@ -64,7 +64,7 @@ type TargetHandler struct {
// NewTargetHandler creates a new handler for the specified client target.
func NewTargetHandler(t client.Target, logf, debugf, errf func(string, ...interface{})) (*TargetHandler, error) {
conn, err := client.Dial(t)
conn, err := client.Dial(t.GetWebsocketURL())
if err != nil {
return nil, err
}