-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathclient_events.go
193 lines (156 loc) · 5.9 KB
/
client_events.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package centrifuge
// ConnectionTokenEvent may contain some useful contextual information in the future.
// For now, it's empty.
type ConnectionTokenEvent struct {
}
// SubscriptionTokenEvent contains info required to get subscription token when
// client wants to subscribe on private channel.
type SubscriptionTokenEvent struct {
Channel string
}
// ServerPublicationEvent has info about received channel Publication.
type ServerPublicationEvent struct {
Channel string
Publication
}
type ServerSubscribedEvent struct {
Channel string
WasRecovering bool
Recovered bool
Recoverable bool
Positioned bool
StreamPosition *StreamPosition
Data []byte
}
// ServerJoinEvent has info about user who left channel.
type ServerJoinEvent struct {
Channel string
ClientInfo
}
// ServerLeaveEvent has info about user who joined channel.
type ServerLeaveEvent struct {
Channel string
ClientInfo
}
// ServerUnsubscribedEvent is an event passed to unsubscribe event handler.
type ServerUnsubscribedEvent struct {
Channel string
}
// ServerSubscribingEvent is an event passed to subscribing event handler.
type ServerSubscribingEvent struct {
Channel string
}
// ConnectedEvent is a connected event context passed to OnConnected callback.
type ConnectedEvent struct {
ClientID string
Version string
Data []byte
}
// ConnectingEvent is a connecting event context passed to OnConnecting callback.
type ConnectingEvent struct {
Code uint32
Reason string
}
// DisconnectedEvent is a disconnected event context passed to OnDisconnected callback.
type DisconnectedEvent struct {
Code uint32
Reason string
}
// ErrorEvent is an error event context passed to OnError callback.
type ErrorEvent struct {
Error error
}
// MessageEvent is an event for async message from server to client.
type MessageEvent struct {
Data []byte
}
// ConnectingHandler is an interface describing how to handle connecting event.
type ConnectingHandler func(ConnectingEvent)
// ConnectedHandler is an interface describing how to handle connect event.
type ConnectedHandler func(ConnectedEvent)
// DisconnectHandler is an interface describing how to handle moveToDisconnected event.
type DisconnectHandler func(DisconnectedEvent)
// MessageHandler is an interface describing how to handle async message from server.
type MessageHandler func(MessageEvent)
// ServerPublicationHandler is an interface describing how to handle Publication from
// server-side subscriptions.
type ServerPublicationHandler func(ServerPublicationEvent)
// ServerSubscribedHandler is an interface describing how to handle subscribe events from
// server-side subscriptions.
type ServerSubscribedHandler func(ServerSubscribedEvent)
// ServerSubscribingHandler is an interface describing how to handle subscribing events for
// server-side subscriptions.
type ServerSubscribingHandler func(ServerSubscribingEvent)
// ServerUnsubscribedHandler is an interface describing how to handle unsubscribe events from
// server-side subscriptions.
type ServerUnsubscribedHandler func(ServerUnsubscribedEvent)
// ServerJoinHandler is an interface describing how to handle Join events from
// server-side subscriptions.
type ServerJoinHandler func(ServerJoinEvent)
// ServerLeaveHandler is an interface describing how to handle Leave events from
// server-side subscriptions.
type ServerLeaveHandler func(ServerLeaveEvent)
// ErrorHandler is an interface describing how to handle error event.
type ErrorHandler func(ErrorEvent)
// eventHub has all event handlers for client.
type eventHub struct {
onConnected ConnectedHandler
onDisconnected DisconnectHandler
onConnecting ConnectingHandler
onError ErrorHandler
onMessage MessageHandler
onServerSubscribe ServerSubscribedHandler
onServerSubscribing ServerSubscribingHandler
onServerUnsubscribed ServerUnsubscribedHandler
onServerPublication ServerPublicationHandler
onServerJoin ServerJoinHandler
onServerLeave ServerLeaveHandler
}
// newEventHub initializes new eventHub.
func newEventHub() *eventHub {
return &eventHub{}
}
// OnConnected is a function to handle connect event.
func (c *Client) OnConnected(handler ConnectedHandler) {
c.events.onConnected = handler
}
// OnConnecting is a function to handle connecting event.
func (c *Client) OnConnecting(handler ConnectingHandler) {
c.events.onConnecting = handler
}
// OnDisconnected is a function to handle moveToDisconnected event.
func (c *Client) OnDisconnected(handler DisconnectHandler) {
c.events.onDisconnected = handler
}
// OnError is a function that will receive unhandled errors for logging.
func (c *Client) OnError(handler ErrorHandler) {
c.events.onError = handler
}
// OnMessage allows processing async message from server to client.
func (c *Client) OnMessage(handler MessageHandler) {
c.events.onMessage = handler
}
// OnPublication sets function to handle Publications from server-side subscriptions.
func (c *Client) OnPublication(handler ServerPublicationHandler) {
c.events.onServerPublication = handler
}
// OnSubscribed sets function to handle server-side subscription subscribe events.
func (c *Client) OnSubscribed(handler ServerSubscribedHandler) {
c.events.onServerSubscribe = handler
}
// OnSubscribing sets function to handle server-side subscription subscribing events.
func (c *Client) OnSubscribing(handler ServerSubscribingHandler) {
c.events.onServerSubscribing = handler
}
// OnUnsubscribed sets function to handle unsubscribe from server-side subscriptions.
func (c *Client) OnUnsubscribed(handler ServerUnsubscribedHandler) {
c.events.onServerUnsubscribed = handler
}
// OnJoin sets function to handle Join event from server-side subscriptions.
func (c *Client) OnJoin(handler ServerJoinHandler) {
c.events.onServerJoin = handler
}
// OnLeave sets function to handle Leave event from server-side subscriptions.
func (c *Client) OnLeave(handler ServerLeaveHandler) {
c.events.onServerLeave = handler
}