Skip to content

Commit

Permalink
add options to customize underlying dialer and request headers (#25)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrey Kaipov <andreykaipov@users.noreply.github.com>
  • Loading branch information
andreykaipov and andreykaipov authored Nov 28, 2021
1 parent dd4892c commit e229173
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
42 changes: 38 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"

Expand All @@ -15,15 +16,19 @@ import (
"github.com/gorilla/websocket"
)

var version = "0.8.0-dev"

// Client represents a client to an OBS websockets server.
type Client struct {
IncomingEvents chan events.Event
*requests.Client

subclients
host string
password string
debug *bool
host string
password string
debug *bool
dialer *websocket.Dialer
requestHeader http.Header
}

// Option represents a functional option of a Client.
Expand Down Expand Up @@ -52,6 +57,26 @@ func WithLogger(x requests.Logger) Option {
}
}

// WithDialer sets the underlying Gorilla WebSocket Dialer (see
// https://pkg.go.dev/github.com/gorilla/websocket#Dialer), should one want to
// customize things like the handshake timeout or TLS configuration. If this is
// not set, it'll use the provided DefaultDialer (see
// https://pkg.go.dev/github.com/gorilla/websocket#pkg-variables).
func WithDialer(x *websocket.Dialer) Option {
return func(o *Client) {
o.dialer = x
}
}

// WithRequestHeader sets custom headers our client can send when trying to
// connect to the WebSockets server, allowing us specify the origin,
// subprotocols, or the user agent.
func WithRequestHeader(x http.Header) Option {
return func(o *Client) {
o.requestHeader = x
}
}

type discard struct{}

func (o *discard) Printf(format string, v ...interface{}) {}
Expand Down Expand Up @@ -86,6 +111,15 @@ func New(host string, opts ...Option) (*Client, error) {
c.Log = &discard{}
}

if c.dialer == nil {
c.dialer = websocket.DefaultDialer
}
if c.requestHeader == nil {
c.requestHeader = http.Header{
"User-Agent": []string{"goobs/" + version},
}
}

if err := c.connect(); err != nil {
return nil, err
}
Expand All @@ -108,7 +142,7 @@ func (c *Client) connect() (err error) {

c.Log.Printf("Connecting to %s", u.String())

if c.Conn, _, err = websocket.DefaultDialer.Dial(u.String(), nil); err != nil {
if c.Conn, _, err = c.dialer.Dial(u.String(), c.requestHeader); err != nil {
return err
}

Expand Down
9 changes: 8 additions & 1 deletion e2e/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package e2e

import (
"net/http"
"testing"

"github.com/andreykaipov/goobs"
Expand All @@ -17,7 +18,13 @@ func check(err error) {
}

func Test_goobs_e2e(t *testing.T) {
client, err := goobs.New("localhost:4545", goobs.WithPassword("hello"))
client, err := goobs.New(
"localhost:4545",
goobs.WithPassword("hello"),
goobs.WithRequestHeader(http.Header{
"User-Agent": []string{"goobs-e2e/0.0.0"},
}),
)
assert.NoError(t, err)

sceneName := "goobs test scene"
Expand Down

0 comments on commit e229173

Please sign in to comment.