-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
41 lines (32 loc) · 917 Bytes
/
client.go
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
package go_sdk
import (
"fmt"
"net"
"github.com/official-stallion/go-sdk/internal"
)
// Client is our client handler which enables a user
// to communicate with broker server.
type Client interface {
// Publish messages to broker
Publish(topic string, data []byte) error
// Subscribe over broker
Subscribe(topic string, handler internal.MessageHandler)
// Unsubscribe from broker
Unsubscribe(topic string)
}
// NewClient creates a new client to connect to broker server.
func NewClient(uri string) (Client, error) {
url, err := urlUnpack(uri)
if err != nil {
return nil, fmt.Errorf("invalid uri: %w", err)
}
conn, err := net.Dial("tcp", url.address)
if err != nil {
return nil, fmt.Errorf("failed to connect to server: %w", err)
}
client, err := internal.NewClient(conn, url.auth)
if err != nil {
return nil, fmt.Errorf("failed to connect to server: %w", err)
}
return client, nil
}