-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathws_conn.go
More file actions
48 lines (43 loc) · 1.29 KB
/
Copy pathws_conn.go
File metadata and controls
48 lines (43 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package metaai
import (
"context"
"time"
"github.com/smart-studio/metaai-go/internal/transport"
)
// clippyConn is the SDK's handle on a connected clippy WebSocket.
// It wraps internal/transport.Conn so the root package stays decoupled from
// gorilla/websocket.
type clippyConn struct {
tc *transport.Conn
}
// dialClippy opens a clippy WebSocket using the client's credentials.
// Returns ErrAccessTokenMissing when there is no ecto1: token to auth with.
func (c *Client) dialClippy(ctx context.Context) (*clippyConn, error) {
if c.accessToken == "" {
return nil, ErrAccessTokenMissing
}
c.logger.Debugf("clippy: dialing clippy WebSocket...")
tc, err := transport.Dial(ctx, transport.DialOptions{
AccessToken: c.accessToken,
CookieHeader: c.CookieHeader(),
UserAgent: c.cfg.UserAgent,
Origin: MetaAIOrigin,
DialTimeout: 30 * time.Second,
})
if err != nil {
c.logger.Debugf("clippy: dial failure: %v", err)
return nil, err
}
c.logger.Debugf("clippy: successfully connected clippy WebSocket")
return &clippyConn{tc: tc}, nil
}
// closeClippy closes the cached clippy connection if any.
func (c *Client) closeClippy() {
c.wsMu.Lock()
defer c.wsMu.Unlock()
if c.ws != nil {
c.logger.Debugf("clippy: closing clippy connection")
_ = c.ws.tc.Close()
c.ws = nil
}
}