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

@ -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
}