-
Notifications
You must be signed in to change notification settings - Fork 14
/
client.go
146 lines (125 loc) · 3.59 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
package centrifuge
import (
"time"
gocentrifuge "github.com/centrifugal/centrifuge-go"
)
// Client to connect to Centrifuge-based server or Centrifugo.
type Client struct {
client *gocentrifuge.Client
}
// Config defaults.
const (
DefaultReadTimeoutMilliseconds = 5000
DefaultWriteTimeoutMilliseconds = 5000
DefaultPingIntervalMilliseconds = 25000
DefaultPrivateChannelPrefix = "$"
)
// Config contains various client options.
type Config struct {
ReadTimeoutMilliseconds int
WriteTimeoutMilliseconds int
PingIntervalMilliseconds int
PrivateChannelPrefix string
}
// DefaultConfig returns Config with default options.
func DefaultConfig() *Config {
return &Config{
PingIntervalMilliseconds: DefaultPingIntervalMilliseconds,
ReadTimeoutMilliseconds: DefaultReadTimeoutMilliseconds,
WriteTimeoutMilliseconds: DefaultWriteTimeoutMilliseconds,
PrivateChannelPrefix: DefaultPrivateChannelPrefix,
}
}
// New initializes Client.
func New(u string, config *Config) *Client {
c := gocentrifuge.Config{
ReadTimeout: time.Duration(config.ReadTimeoutMilliseconds) * time.Millisecond,
WriteTimeout: time.Duration(config.WriteTimeoutMilliseconds) * time.Millisecond,
PingInterval: time.Duration(config.PingIntervalMilliseconds) * time.Millisecond,
PrivateChannelPrefix: config.PrivateChannelPrefix,
}
client := &Client{
client: gocentrifuge.New(u, c),
}
return client
}
// SetToken allows to set connection token so client
// authenticate itself on connect.
func (c *Client) SetToken(token string) {
c.client.SetToken(token)
}
// SetName allows to set client name.
func (c *Client) SetName(name string) {
c.client.SetName(name)
}
// SetVersion allows to set client version.
func (c *Client) SetVersion(version string) {
c.client.SetVersion(version)
}
// SetConnectData allows to set data to send in connect message.
func (c *Client) SetConnectData(data []byte) {
c.client.SetConnectData(data)
}
// SetHeader allows to set custom header sent in Upgrade HTTP request.
func (c *Client) SetHeader(key, value string) {
c.client.SetHeader(key, value)
}
// Send data to server asynchronously.
func (c *Client) Send(data []byte) error {
return c.client.Send(data)
}
type RPCResult struct {
Data []byte
}
// RPC allows to make RPC – send data to server ant wait for response.
// RPC handler must be registered on server.
func (c *Client) RPC(data []byte) (*RPCResult, error) {
res, err := c.client.RPC(data)
if err != nil {
return nil, err
}
return &RPCResult{
Data: res.Data,
}, nil
}
// NamedRPC allows to make RPC with method.
func (c *Client) NamedRPC(method string, data []byte) (*RPCResult, error) {
res, err := c.client.NamedRPC(method, data)
if err != nil {
return nil, err
}
return &RPCResult{
Data: res.Data,
}, nil
}
// Close closes Client connection and cleans ups everything.
func (c *Client) Close() error {
return c.client.Close()
}
// Connect dials to server and sends connect message.
func (c *Client) Connect() error {
return c.client.Connect()
}
// Disconnect client from server.
func (c *Client) Disconnect() error {
return c.client.Disconnect()
}
// Publish data into channel.
func (c *Client) Publish(channel string, data []byte) (*PublishResult, error) {
_, err := c.client.Publish(channel, data)
if err != nil {
return nil, err
}
return &PublishResult{}, nil
}
// NewSubscription allows to create new Subscription to channel.
func (c *Client) NewSubscription(channel string) (*Subscription, error) {
sub, err := c.client.NewSubscription(channel)
if err != nil {
return nil, err
}
s := &Subscription{
sub: sub,
}
return s, nil
}