-
Notifications
You must be signed in to change notification settings - Fork 3
/
channel.go
51 lines (40 loc) · 1004 Bytes
/
channel.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
package pubnub
// Channel is used for established subscription connections
type Channel struct {
Name string
messageCh chan Message
errorCh chan error
pr *PubNubRequest
}
// NewChannel opens a new subscription channel
func (p *PubNubClient) NewChannel(name string) (*Channel, error) {
if name == "" {
return nil, ErrChannelNotSet
}
channel := &Channel{
Name: name,
messageCh: make(chan Message),
errorCh: make(chan error),
}
// channel not found
pr := NewPubNubRequest(name, channel.messageCh, channel.errorCh)
channel.pr = pr
go pr.handleResponse()
// timetoken parameter is not sent for now
go p.pub.Subscribe(name, "", pr.successCh, false, pr.errorCh)
if err := pr.Do(); err != nil {
return nil, err
}
return channel, nil
}
func (c *Channel) Consume() <-chan Message {
return c.messageCh
}
func (c *Channel) ConsumeErrors() <-chan error {
return c.errorCh
}
func (c *Channel) Close() {
c.pr.Close()
close(c.messageCh)
close(c.errorCh)
}