From 4ae10864e4f58a80eeb5a09ac10b681be9c72379 Mon Sep 17 00:00:00 2001 From: Kenneth Shaw Date: Fri, 13 Jul 2018 11:24:37 +0700 Subject: [PATCH] Adding client API changes prior to package rewrite --- client/chrome.go | 10 ++++++++-- client/client.go | 1 + client/transport.go | 10 ++++------ handler.go | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/client/chrome.go b/client/chrome.go index 62d6994..946ca5a 100644 --- a/client/chrome.go +++ b/client/chrome.go @@ -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 { diff --git a/client/client.go b/client/client.go index cc4303c..1e957d6 100644 --- a/client/client.go +++ b/client/client.go @@ -50,6 +50,7 @@ type Target interface { String() string GetID() string GetType() TargetType + GetDevtoolsURL() string GetWebsocketURL() string } diff --git a/client/transport.go b/client/transport.go index 548f03c..50cd15d 100644 --- a/client/transport.go +++ b/client/transport.go @@ -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 ... diff --git a/handler.go b/handler.go index 4e02b5f..4982087 100644 --- a/handler.go +++ b/handler.go @@ -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 }