-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsubscription.go
147 lines (128 loc) · 3.13 KB
/
subscription.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
package centrifuge
import (
gocentrifuge "github.com/centrifugal/centrifuge-go"
)
// Publication ...
type Publication struct {
Offset int64
Data []byte
Info *ClientInfo
}
// ClientInfo ...
type ClientInfo struct {
Client string
User string
ConnInfo []byte
ChanInfo []byte
}
// PresenceStats ...
type PresenceStats struct {
NumClients int
NumUsers int
}
// Subscription describes client subscription to channel.
type Subscription struct {
sub *gocentrifuge.Subscription
}
// Channel returns subscription channel.
func (s *Subscription) Channel() string {
return s.sub.Channel()
}
type PublishResult struct{}
// Publish allows to publish JSON encoded data to subscription channel.
func (s *Subscription) Publish(data []byte) (*PublishResult, error) {
_, err := s.sub.Publish(data)
if err != nil {
return nil, err
}
return &PublishResult{}, nil
}
// Unsubscribe allows to unsubscribe from channel.
func (s *Subscription) Unsubscribe() error {
return s.sub.Unsubscribe()
}
// Subscribe allows to subscribe again after unsubscribing.
func (s *Subscription) Subscribe() error {
return s.sub.Subscribe()
}
// HistoryData ...
type HistoryData struct {
publications []gocentrifuge.Publication
}
// NumItems to get total number of Publication items in collection.
func (d *HistoryData) NumItems() int {
return len(d.publications)
}
// ItemAt to get Publication by index.
func (d *HistoryData) ItemAt(i int) *Publication {
pub := d.publications[i]
var info *ClientInfo
if pub.Info != nil {
info = &ClientInfo{
Client: pub.Info.Client,
User: pub.Info.User,
ConnInfo: pub.Info.ConnInfo,
ChanInfo: pub.Info.ChanInfo,
}
}
return &Publication{
Offset: int64(pub.Offset),
Data: pub.Data,
Info: info,
}
}
// History allows to extract channel history.
func (s *Subscription) History() (*HistoryData, error) {
res, err := s.sub.History()
if err != nil {
return nil, err
}
return &HistoryData{
publications: res.Publications,
}, nil
}
// PresenceData contains presence information for channel.
type PresenceData struct {
clients []gocentrifuge.ClientInfo
}
// NumItems to get total number of ClientInfo items in collection.
func (d *PresenceData) NumItems() int {
return len(d.clients)
}
// ItemAt to get ClientInfo by index.
func (d *PresenceData) ItemAt(i int) *ClientInfo {
info := d.clients[i]
return &ClientInfo{
Client: info.Client,
User: info.User,
ConnInfo: info.ConnInfo,
ChanInfo: info.ChanInfo,
}
}
// Presence allows to extract presence information for channel.
func (s *Subscription) Presence() (*PresenceData, error) {
res, err := s.sub.Presence()
if err != nil {
return nil, err
}
clients := make([]gocentrifuge.ClientInfo, len(res.Presence))
i := 0
for _, info := range res.Presence {
clients[i] = info
i++
}
return &PresenceData{
clients: clients,
}, nil
}
// PresenceStats allows to extract presence stats information for channel.
func (s *Subscription) PresenceStats() (*PresenceStats, error) {
stats, err := s.sub.PresenceStats()
if err != nil {
return nil, err
}
return &PresenceStats{
NumClients: stats.NumClients,
NumUsers: stats.NumUsers,
}, nil
}