This commit is contained in:
47
obsws/client_requests.go
Normal file
47
obsws/client_requests.go
Normal file
@ -0,0 +1,47 @@
|
||||
package obsws
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
const interval = time.Millisecond * 50
|
||||
|
||||
var (
|
||||
// ErrNotConnected is returned when a request is sent by a client which is not connected.
|
||||
ErrNotConnected = errors.New("not connected")
|
||||
// ErrReceiveTimeout is returned when a response takes too long to arrive.
|
||||
ErrReceiveTimeout = errors.New("receive timed out")
|
||||
)
|
||||
|
||||
// sendRequest sends a request to the WebSocket server.
|
||||
func (c *Client) sendRequest(req Request) (chan map[string]interface{}, error) {
|
||||
if !c.connected {
|
||||
return nil, ErrNotConnected
|
||||
}
|
||||
future := make(chan map[string]interface{})
|
||||
if err := c.conn.WriteJSON(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
Logger.Println("sent request", req.ID())
|
||||
go func() { future <- c.receive(req.ID()) }()
|
||||
return future, nil
|
||||
}
|
||||
|
||||
// receive waits until a response matching the given ID arrives.
|
||||
func (c *Client) receive(id string) map[string]interface{} {
|
||||
for {
|
||||
resp := <-c.respQ
|
||||
if resp["message-id"] == id {
|
||||
Logger.Println("received response", resp["message-id"])
|
||||
return resp
|
||||
}
|
||||
c.respQ <- resp
|
||||
time.Sleep(interval)
|
||||
}
|
||||
}
|
||||
|
||||
// handleResponse sends a response into the queue.
|
||||
func (c *Client) handleResponse(m map[string]interface{}) {
|
||||
c.respQ <- m
|
||||
}
|
Reference in New Issue
Block a user