-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
157 lines (118 loc) · 3.31 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package pubnub
import (
"errors"
"sync"
"github.com/pubnub/go/messaging"
)
var (
ErrChannelNotSet = errors.New("channel name is not set")
ErrTimeout = errors.New("timeout reached")
ErrConnectionAbort = errors.New("connection aborted")
ErrConnectionClosed = errors.New("connection closed")
)
type PubNubClient struct {
pub *messaging.Pubnub
channels map[string]*Channel
mu sync.RWMutex
closed bool
}
func NewPubNubClient(cs *ClientSettings) *PubNubClient {
pub := messaging.NewPubnub(cs.PublishKey, cs.SubscribeKey, cs.SecretKey, cs.Cipher, cs.SSL, cs.ID, nil)
return &PubNubClient{
pub: pub,
channels: make(map[string]*Channel),
}
}
////////////// ClientSettings ////////////////////
type ClientSettings struct {
PublishKey string
SubscribeKey string
SecretKey string
Cipher string
SSL bool
ID string
}
// Push sends a message to the channel with channelName. If Access Manager is enabled
// access must be granted first.
func (p *PubNubClient) Push(channelName string, body interface{}) error {
if p.closed {
return ErrConnectionClosed
}
pr := NewPubNubRequest(channelName, nil, nil)
defer pr.Close()
go pr.handleResponse()
go p.pub.Publish(channelName, body, pr.successCh, pr.errorCh)
return pr.Do()
}
// Grant read/write access to the given token for TTL period. If token
func (p *PubNubClient) Grant(a *AuthSettings) error {
if p.closed {
return ErrConnectionClosed
}
if a.ChannelName == "" {
return ErrChannelNotSet
}
pr := NewPubNubRequest(a.ChannelName, nil, nil)
defer pr.Close()
p.pub.SetAuthenticationKey(a.Token)
go pr.handleResponse()
// channel name, read access, write access, TTL, success channel, error channel
go p.pub.GrantSubscribe(a.ChannelName, a.CanRead, a.CanWrite, a.TTL, a.Token, pr.successCh, pr.errorCh)
return pr.Do()
}
// Subscribe to given channel.
// Returns a message listener channel
// TODO add support for multiple channel subscription
func (p *PubNubClient) Subscribe(channelName string) (*Channel, error) {
if p.closed {
return nil, ErrConnectionClosed
}
if channelName == "" {
return nil, ErrChannelNotSet
}
return p.fetchOrCreateChannel(channelName)
}
func (p *PubNubClient) Close() {
p.pub.CloseExistingConnection()
for _, channel := range p.channels {
channel.Close()
}
p.closed = true
}
func (p *PubNubClient) SetAuthToken(token string) {
p.pub.SetAuthenticationKey(token)
}
func (p *PubNubClient) fetchOrCreateChannel(channelName string) (*Channel, error) {
p.mu.RLock()
channel, ok := p.channels[channelName]
p.mu.RUnlock()
if ok {
return channel, nil
}
channel, err := p.NewChannel(channelName)
if err != nil {
return nil, err
}
p.mu.Lock()
p.channels[channelName] = channel
p.mu.Unlock()
return channel, nil
}
//////////////////// PubnubAuthSettings /////////////////////
type AuthSettings struct {
// PubNub channel name
ChannelName string
// Grant access for the token. When token is an
// empty string it provides public access for the channel.
Token string
// Grant read access
CanRead bool
// Grant write Access
CanWrite bool
// Time to live value in minutes.
// Access is revoked after TTL period
// Min-max values can be consecutively: 1 and 525600
// 0 value will grant access indefinitely
// -1 causes default value (1440) to be set
TTL int
}