-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
99 lines (88 loc) · 2.71 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package goqtt
import (
"fmt"
"net"
"os"
"strconv"
"time"
)
// Default values for the Client configuration. See the DefaultClientId function for
// the Client's default ClientId value.
const (
DefaultPort = "1883"
DefaultKeepAlive = 60 // seconds
DefaultTopic = "goqtt"
)
// clientConfig holds the information needed to create a new Client.
// As it's unexported these can't be set directly, instead use a functional
// option, like Port.
type clientConfig struct {
clientId string
keepAlive int
// server is the address (IP or domain) of the broker.
server string
port string
topic string
}
// Client is the main interaction point for sending and receiving Packets. An
// instantiated Client needs to call the Connect() method before sending/receiving any packets.
type Client struct {
Config *clientConfig
conn net.Conn
}
// NewClient creates a Client struct which can be used to interact with a MQTT broker.
// It sets default values for most of the configuration, so only a server address is
// required to instantiate it. Other configuration options are available and can be
// passed in as needed.
func NewClient(addr string, opts ...option) *Client {
var c = &Client{}
// default configuration
d := &clientConfig{
clientId: DefaultClientId(),
keepAlive: DefaultKeepAlive,
server: addr,
port: DefaultPort,
topic: DefaultTopic,
}
c.Config = d
// apply any other options that have been passed in
for _, opt := range opts {
opt(c)
}
return c
}
// DefaultClientId creates a default value for the Client's clientId.
// It uses the process id and the current time to try to prevent client collisions with the broker.
func DefaultClientId() string {
cid := fmt.Sprintf("%s-%d-%s", "goqtt", os.Getpid(), strconv.Itoa(time.Now().Second()))
return cid
}
// option is a function used to modify/set a configuration field in a Client.
type option func(*Client)
// ClientId is the identifier you use to tell the MQTT broker who you are.
// A broker will require unique ClientId's for all connected clients.
func ClientId(cid string) option {
return func(c *Client) {
c.Config.clientId = cid
}
}
// KeepAlive is the time in seconds that the broker should wait before
// disconnecting you if no packets are sent.
func KeepAlive(seconds int) option {
return func(c *Client) {
c.Config.keepAlive = seconds
}
}
// Port is the port number of the server. Usually 1883 (insecure) or 8883 (TLS).
func Port(port string) option {
return func(c *Client) {
c.Config.port = port
}
}
// Topic is the fully qualified topic to subscribe or publish to.
// Only single topics and no wildcards are accepted at this time.
func Topic(topic string) option {
return func(c *Client) {
c.Config.topic = topic
}
}