goobs

package module
v0.12.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2023 License: Apache-2.0 Imports: 30 Imported by: 0

README

goobs

Protocol Version Documentation Build Status Go Report

It's a Go client for obsproject/obs-websocket, allowing us to interact with OBS Studio from Go!

installation

To add this library to your module, simply go get it like any other Go module after you've initialized your own:

❯ go mod init blah
❯ go get github.com/onfield/goobs

usage

Here's a basic example, where we grab the version and print out the scenes. Check out the examples for more.

package main

import (
	"fmt"
	"log"

	"github.com/onfield/goobs"
)

func main() {
	client, err := goobs.New("localhost:4455", goobs.WithPassword("goodpassword"))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Disconnect()

	version, err := client.General.GetVersion()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("OBS Studio version: %s\n", version.ObsVersion)
	fmt.Printf("Websocket server version: %s\n", version.ObsWebSocketVersion)

	resp, _ := client.Scenes.GetSceneList()
	for _, v := range resp.Scenes {
		fmt.Printf("%2d %s\n", v.SceneIndex, v.SceneName)
	}
}

This outputs the following:

❯ go run examples/basic/main.go
OBS Studio version: 29.0.0
Websocket server version: 5.1.0
 1 Just Chatting
 2 Intermission
 3 Be Right Back 2
 4 Be Right Back
 5 Stream Starting Soon
 6 Background
 7 Camera Secondary
 8 Camera Primary
 9 Main 2
10 Main
logging

Further, we can view what this library is doing under the hood (i.e. the raw messages it sends and receives) by setting GOOBS_LOG=debug. This value can be set to the typical Log4j values (e.g. debug, info, error) for more or less verbosity.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*api.Client
	// contains filtered or unexported fields
}

Client represents a client to an OBS websockets server.

func New

func New(host string, opts ...Option) (*Client, error)

New creates and configures a client to interact with the OBS websockets server. It also opens up a connection, so be sure to check the error.

func (*Client) Connected added in v0.12.6

func (c *Client) Connected() bool

Still connected?

func (*Client) Disconnect

func (c *Client) Disconnect() error

Disconnect sends a message to the OBS websocket server to close the client's open connection. You don't really have to do this as any connections should close when your program terminates or interrupts. But here's a function anyways.

func (*Client) Listen

func (c *Client) Listen(f func(interface{}))

type Option

type Option func(*Client)

Option represents a functional option of a Client.

func WithDialer

func WithDialer(x *websocket.Dialer) 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 WithEventSubscriptions

func WithEventSubscriptions(x int) Option

WithEventSubscriptions specifies the events we'd like to susbcribe to via `client.Listen()`. The value is a bitmask of any of the subscription values specified in api/events/subscriptions. By default, all event categories are subscribed, except for events marked as high volume. High volume events must be explicitly subscribed to.

func WithLogger

func WithLogger(x api.Logger) Option

WithLogger sets the logger this library will use. See the logger.Logger interface. Should be compatible with most third-party loggers.

func WithPassword

func WithPassword(x string) Option

WithPassword sets the password of a client.

func WithRequestHeader

func WithRequestHeader(x http.Header) Option

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 WithResponseTimeout

func WithResponseTimeout(x time.Duration) Option

WithResponseTimeout sets the time we're willing to wait to receive a response from the server for any request, before responding with an error. It's in milliseconds. The default timeout is 10 seconds.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL