Add auth support
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is failing

This commit is contained in:
Tyler 2020-06-27 16:59:56 -04:00
parent b154704f1d
commit 2baa15923a
6 changed files with 64 additions and 19 deletions

View File

@ -8,13 +8,14 @@ import (
type SlobsClient struct {
client *slobs.Client
key string
password string
recording bool
}
func NewSlobsClient(key, host string, port int, password string) *SlobsClient {
slobsc := slobs.NewClient(host + ":" + strconv.Itoa(port))
return &SlobsClient{client: slobsc, key: key}
return &SlobsClient{client: slobsc, key: key, password: password}
}
const (
@ -31,6 +32,19 @@ func (c *SlobsClient) Connect() error {
return err
}
c.client.Auth(c.password, func(err error) {
if err != nil {
// TODO alert that it failed to startup?
return
}
c.requestInitialData()
})
return nil
}
func (c *SlobsClient) requestInitialData() {
c.client.SendRPC("StreamingService", "getModel", func(e *slobs.RPCResponse) {
state := &slobs.IStreamingState{}
@ -63,8 +77,6 @@ func (c *SlobsClient) Connect() error {
c.recording = false
}
})
return nil
}
func (c *SlobsClient) Disconnect() error {

2
go.mod
View File

@ -3,11 +3,9 @@ module meow.tf/streamdeck/obs-replay
go 1.13
require (
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9
github.com/gorilla/websocket v1.4.1
github.com/mitchellh/mapstructure v1.1.2
github.com/mordillo123/sockjs-go-client v0.0.0-20161009150606-c1486c966a40
github.com/valyala/fastjson v1.4.1
meow.tf/streamdeck/sdk v0.0.0-20190519021527-54a933f8777d
)

4
go.sum
View File

@ -1,13 +1,9 @@
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9 h1:74lLNRzvsdIlkTgfDSMuaPjBr4cf6k7pwQQANm/yLKU=
github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9/go.mod h1:GgB8SF9nRG+GqaDtLcwJZsQFhcogVCJ79j4EdT0c2V4=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mordillo123/sockjs-go-client v0.0.0-20161009150606-c1486c966a40 h1:d/FazCBU+C1trw+F+Gdah5Q3QX5kNsOigUCes3z0gVc=
github.com/mordillo123/sockjs-go-client v0.0.0-20161009150606-c1486c966a40/go.mod h1:b+6Qgn9fCwiTtpRr6lHe51DY2g69kcMnWc53J4EV4Jc=
github.com/valyala/fastjson v1.4.1 h1:hrltpHpIpkaxll8QltMU8c3QZ5+qIiCL8yKqPFJI/yE=
github.com/valyala/fastjson v1.4.1/go.mod h1:nV6MsjxL2IMJQUoHDIrjEI7oLyeqK6aBD7EFWPsvP8o=
meow.tf/streamdeck/sdk v0.0.0-20190519021527-54a933f8777d h1:PPZHRoZFy9p4GjXssLvTneJfX6cS0bEm51md5TqXFgU=

View File

@ -42,7 +42,7 @@
"Icon": "images/pluginIcon",
"CategoryIcon": "images/pluginIcon",
"URL": "https://streamdeck.meow.tf/obsreplay",
"Version": "1.1.1",
"Version": "1.1.2",
"SDKVersion": 2,
"OS": [
{

View File

@ -85,12 +85,38 @@ func (c *Client) Connect() error {
return nil
}
func (c *Client) Auth(key string, callback func(error)) {
c.SendRPC("TcpServerService", "auth", func(response *RPCResponse) {
if response.Error != nil {
callback(errors.New(response.Error.Message))
return
}
var result bool
json.Unmarshal(*response.Result, &result)
if !result {
return
}
callback(nil)
}, key)
}
func (c *Client) Disconnect() error {
return c.conn.Close()
}
func (c *Client) Subscribe(resource, method string, handler SubscriptionHandler) {
func (c *Client) Subscribe(resource, method string, handler SubscriptionHandler) error {
responseCh := make(chan error, 1)
c.SendRPC(resource, method, func(response *RPCResponse) {
if response.Error != nil {
responseCh <- errors.New(response.Error.Message)
return
}
res := &ResourceEvent{}
json.Unmarshal(*response.Result, &res)
@ -98,14 +124,18 @@ func (c *Client) Subscribe(resource, method string, handler SubscriptionHandler)
c.subscriptionLock.Lock()
c.subscriptions[res.ResourceId] = handler
c.subscriptionLock.Unlock()
close(responseCh)
})
return <-responseCh
}
func (c *Client) SendRPC(resource, method string, handler ResponseHandler) error {
func (c *Client) SendRPC(resource, method string, handler ResponseHandler, args ...string) error {
m := make(map[string]interface{})
m["resource"] = resource
m["args"] = []string{}
m["args"] = args
atomic.AddInt32(&c.requestId, 1)

View File

@ -14,12 +14,21 @@ func Test_StreamlabsOBS(t *testing.T) {
t.Fatal(err)
}
c.Subscribe("StreamingService", "replayBufferStatusChange", func(event *ResourceEvent) {
var status string
event.DecodeTo(&status)
log.Println("Event received:", status)
closeCh := make(chan struct{}, 1)
c.Auth("a", func(err error) {
if err != nil {
t.Fatal(err)
closeCh <- struct{}{}
return
}
c.Subscribe("StreamingService", "replayBufferStatusChange", func(event *ResourceEvent) {
var status string
event.DecodeTo(&status)
log.Println("Event received:", status)
})
})
ch := make(chan struct{}, 1)
<-ch
<-closeCh
}