forked from centrifugal/centrifuge-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
118 lines (100 loc) · 3.1 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
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
events *EventHub
}
// 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),
events: NewEventHub(),
}
return client
}
// SetToken allows to set connection token so client
// authenticate itself on connect.
func (c *Client) SetToken(token string) {
c.client.SetToken(token)
}
// 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)
}
// 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) ([]byte, error) {
return c.client.RPC(data)
}
// NamedRPC allows to make RPC with method.
func (c *Client) NamedRPC(method string, data []byte) ([]byte, error) {
return c.client.NamedRPC(method, data)
}
// 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) error {
return c.client.Publish(channel, data)
}
// 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
}