| 1 |
package centrifuge |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
) |
| 6 |
|
| 7 |
// ConnectEvent contains fields related to connecting event (when server |
| 8 |
// received Connect protocol command from client). |
| 9 |
type ConnectEvent struct { |
| 10 |
// ClientID that was generated by library for client connection. |
| 11 |
ClientID string |
| 12 |
// Token received from client as part of Connect Command. |
| 13 |
Token string |
| 14 |
// Data received from client as part of Connect Command. |
| 15 |
Data []byte |
| 16 |
// Name can contain client name if provided on connect. |
| 17 |
Name string |
| 18 |
// Version can contain client version if provided on connect. |
| 19 |
Version string |
| 20 |
// Transport contains information about transport used by client. |
| 21 |
Transport TransportInfo |
| 22 |
} |
| 23 |
|
| 24 |
// ConnectReply contains reaction to ConnectEvent. |
| 25 |
type ConnectReply struct { |
| 26 |
// Context allows to return modified context. |
| 27 |
Context context.Context |
| 28 |
// Credentials should be set if app wants to authenticate connection. |
| 29 |
// This field is optional since auth Credentials could be set through |
| 30 |
// HTTP middleware. |
| 31 |
Credentials *Credentials |
| 32 |
// Data allows to set custom data in connect reply. |
| 33 |
Data []byte |
| 34 |
// Subscriptions map contains channels to subscribe connection to on server-side. |
| 35 |
Subscriptions map[string]SubscribeOptions |
| 36 |
// ClientSideRefresh tells library to use client-side refresh logic: |
| 37 |
// i.e. send refresh commands with new connection token. If not set |
| 38 |
// then server-side refresh mechanism will be used. |
| 39 |
ClientSideRefresh bool |
| 40 |
} |
| 41 |
|
| 42 |
// ConnectingHandler called when new client authenticates on server. |
| 43 |
type ConnectingHandler func(context.Context, ConnectEvent) (ConnectReply, error) |
| 44 |
|
| 45 |
// ConnectHandler called when client connected to server and ready to communicate. |
| 46 |
type ConnectHandler func(*Client) |
| 47 |
|
| 48 |
// RefreshEvent contains fields related to refresh event. |
| 49 |
type RefreshEvent struct { |
| 50 |
// ClientSideRefresh is true for refresh initiated by client-side refresh workflow. |
| 51 |
ClientSideRefresh bool |
| 52 |
// Token will only be set in case of using client-side refresh mechanism. |
| 53 |
Token string |
| 54 |
} |
| 55 |
|
| 56 |
// RefreshReply contains fields determining the reaction on refresh event. |
| 57 |
type RefreshReply struct { |
| 58 |
// Expired tells Centrifuge that connection expired. In this case connection will be |
| 59 |
// closed with DisconnectExpired. |
| 60 |
Expired bool |
| 61 |
// ExpireAt defines time in future when connection should expire, |
| 62 |
// zero value means no expiration. |
| 63 |
ExpireAt int64 |
| 64 |
// Info allows to modify connection information, |
| 65 |
// zero value means no modification of current connection Info. |
| 66 |
Info []byte |
| 67 |
} |
| 68 |
|
| 69 |
// RefreshCallback should be called as soon as handler decides what to do |
| 70 |
// with connection refresh event. |
| 71 |
type RefreshCallback func(RefreshReply, error) |
| 72 |
|
| 73 |
// RefreshHandler called when it's time to validate client connection and |
| 74 |
// update it's expiration time if it's still actual. |
| 75 |
// |
| 76 |
// Centrifuge library supports two ways of refreshing connection: client-side |
| 77 |
// and server-side. |
| 78 |
// |
| 79 |
// The default mechanism is server-side, this means that as soon refresh handler |
| 80 |
// set and connection expiration time happens (by timer) – refresh handler will |
| 81 |
// be called. |
| 82 |
// |
| 83 |
// If ClientSideRefresh in ConnectReply inside ConnectingHandler set to true then |
| 84 |
// library uses client-side refresh mechanism. In this case library relies on |
| 85 |
// Refresh commands sent from client periodically to refresh connection. Refresh |
| 86 |
// command contains updated connection token. |
| 87 |
type RefreshHandler func(RefreshEvent, RefreshCallback) |
| 88 |
|
| 89 |
// AliveHandler called periodically while connection alive. This is a helper |
| 90 |
// to do periodic things which can tolerate some approximation in time. This |
| 91 |
// callback will run every ClientPresenceUpdateInterval and can save you a timer. |
| 92 |
type AliveHandler func() |
| 93 |
|
| 94 |
// DisconnectEvent contains fields related to disconnect event. |
| 95 |
type DisconnectEvent struct { |
| 96 |
// Disconnect can optionally contain a custom disconnect object that |
| 97 |
// was sent from server to client with closing handshake. If this field |
| 98 |
// exists then client connection was closed from server. If this field |
| 99 |
// is nil then this means that client disconnected normally and connection |
| 100 |
// closing was initiated by client side. |
| 101 |
Disconnect *Disconnect |
| 102 |
} |
| 103 |
|
| 104 |
// DisconnectHandler called when client disconnects from server. The important |
| 105 |
// thing to remember is that you should not rely entirely on this handler to |
| 106 |
// clean up non-expiring resources (in your database for example). Why? Because |
| 107 |
// in case of any non-graceful node shutdown (kill -9, process crash, machine lost) |
| 108 |
// disconnect handler will never be called (obviously) so you can have stale data. |
| 109 |
type DisconnectHandler func(DisconnectEvent) |
| 110 |
|
| 111 |
// SubscribeEvent contains fields related to subscribe event. |
| 112 |
type SubscribeEvent struct { |
| 113 |
// Channel client wants to subscribe to. |
| 114 |
Channel string |
| 115 |
// Token will only be set for token channels. This is a task of application |
| 116 |
// to check that subscription to a channel has valid token. |
| 117 |
Token string |
| 118 |
} |
| 119 |
|
| 120 |
// SubscribeCallback should be called as soon as handler decides what to do |
| 121 |
// with connection subscribe event. |
| 122 |
type SubscribeCallback func(SubscribeReply, error) |
| 123 |
|
| 124 |
// SubscribeReply contains fields determining the reaction on subscribe event. |
| 125 |
type SubscribeReply struct { |
| 126 |
// Options to control subscription. |
| 127 |
Options SubscribeOptions |
| 128 |
|
| 129 |
// ClientSideRefresh tells library to use client-side refresh logic: i.e. send |
| 130 |
// SubRefresh commands with new Subscription Token. If not set then server-side |
| 131 |
// SubRefresh handler will be used. |
| 132 |
ClientSideRefresh bool |
| 133 |
} |
| 134 |
|
| 135 |
// SubscribeHandler called when client wants to subscribe on channel. |
| 136 |
type SubscribeHandler func(SubscribeEvent, SubscribeCallback) |
| 137 |
|
| 138 |
// UnsubscribeEvent contains fields related to unsubscribe event. |
| 139 |
type UnsubscribeEvent struct { |
| 140 |
// Channel client unsubscribed from. |
| 141 |
Channel string |
| 142 |
} |
| 143 |
|
| 144 |
// UnsubscribeHandler called when client unsubscribed from channel. |
| 145 |
type UnsubscribeHandler func(UnsubscribeEvent) |
| 146 |
|
| 147 |
// PublishEvent contains fields related to publish event. Note that this event |
| 148 |
// called before actual publish to Broker so handler has an option to reject this |
| 149 |
// publication returning an error. |
| 150 |
type PublishEvent struct { |
| 151 |
// Channel client wants to publish data to. |
| 152 |
Channel string |
| 153 |
// Data client wants to publish. |
| 154 |
Data []byte |
| 155 |
// ClientInfo about client connection. |
| 156 |
ClientInfo *ClientInfo |
| 157 |
} |
| 158 |
|
| 159 |
// PublishReply contains fields determining the result on publish. |
| 160 |
type PublishReply struct { |
| 161 |
// Options to control publication. |
| 162 |
Options PublishOptions |
| 163 |
|
| 164 |
// Result if set will tell Centrifuge that message already published to |
| 165 |
// channel by handler code. In this case Centrifuge won't try to publish |
| 166 |
// into channel again after handler returned PublishReply. This can be |
| 167 |
// useful if you need to know new Publication offset in your code or you |
| 168 |
// want to make sure message successfully published to Broker on server |
| 169 |
// side (otherwise only client will get an error). |
| 170 |
Result *PublishResult |
| 171 |
} |
| 172 |
|
| 173 |
// PublishCallback should be called with PublishReply or error. |
| 174 |
type PublishCallback func(PublishReply, error) |
| 175 |
|
| 176 |
// PublishHandler called when client publishes into channel. |
| 177 |
type PublishHandler func(PublishEvent, PublishCallback) |
| 178 |
|
| 179 |
// SubRefreshEvent contains fields related to subscription refresh event. |
| 180 |
type SubRefreshEvent struct { |
| 181 |
// ClientSideRefresh is true for refresh initiated by client-side subscription |
| 182 |
// refresh workflow. |
| 183 |
ClientSideRefresh bool |
| 184 |
// Channel to which SubRefreshEvent belongs to. |
| 185 |
Channel string |
| 186 |
// Token will only be set in case of using client-side subscription refresh mechanism. |
| 187 |
Token string |
| 188 |
} |
| 189 |
|
| 190 |
// SubRefreshReply contains fields determining the reaction on |
| 191 |
// subscription refresh event. |
| 192 |
type SubRefreshReply struct { |
| 193 |
// Expired tells Centrifuge that subscription expired. In this case connection will be |
| 194 |
// closed with DisconnectExpired. |
| 195 |
Expired bool |
| 196 |
// ExpireAt is a new Unix time of expiration. Zero value means no expiration. |
| 197 |
ExpireAt int64 |
| 198 |
// Info is a new channel-scope info. Zero value means do not change previous one. |
| 199 |
Info []byte |
| 200 |
} |
| 201 |
|
| 202 |
// SubRefreshCallback should be called as soon as handler decides what to do |
| 203 |
// with connection SubRefreshEvent. |
| 204 |
type SubRefreshCallback func(SubRefreshReply, error) |
| 205 |
|
| 206 |
// SubRefreshHandler called when it's time to validate client subscription to channel and |
| 207 |
// update it's state if needed. |
| 208 |
// |
| 209 |
// If ClientSideRefresh in SubscribeReply inside SubscribeHandler set to true then |
| 210 |
// library uses client-side subscription refresh mechanism. In this case library relies on |
| 211 |
// SubRefresh commands sent from client periodically to refresh subscription. SubRefresh |
| 212 |
// command contains updated subscription token. |
| 213 |
type SubRefreshHandler func(SubRefreshEvent, SubRefreshCallback) |
| 214 |
|
| 215 |
// RPCEvent contains fields related to rpc request. |
| 216 |
type RPCEvent struct { |
| 217 |
// Method is an optional string that contains RPC method name client wants to call. |
| 218 |
// This is an optional field, by default clients send RPC without any method set. |
| 219 |
Method string |
| 220 |
// Data contains RPC untouched payload. |
| 221 |
Data []byte |
| 222 |
} |
| 223 |
|
| 224 |
// RPCReply contains fields determining the reaction on rpc request. |
| 225 |
type RPCReply struct { |
| 226 |
// Data to return in RPC reply to client. |
| 227 |
Data []byte |
| 228 |
} |
| 229 |
|
| 230 |
// RPCCallback should be called as soon as handler decides what to do |
| 231 |
// with connection RPCEvent. |
| 232 |
type RPCCallback func(RPCReply, error) |
| 233 |
|
| 234 |
// RPCHandler must handle incoming command from client. |
| 235 |
type RPCHandler func(RPCEvent, RPCCallback) |
| 236 |
|
| 237 |
// MessageEvent contains fields related to message request. |
| 238 |
type MessageEvent struct { |
| 239 |
// Data contains message untouched payload. |
| 240 |
Data []byte |
| 241 |
} |
| 242 |
|
| 243 |
// MessageHandler must handle incoming async message from client. |
| 244 |
type MessageHandler func(MessageEvent) |
| 245 |
|
| 246 |
// PresenceEvent has channel operation called for. |
| 247 |
type PresenceEvent struct { |
| 248 |
Channel string |
| 249 |
} |
| 250 |
|
| 251 |
// PresenceReply contains fields determining the reaction on presence request. |
| 252 |
type PresenceReply struct { |
| 253 |
Result *PresenceResult |
| 254 |
} |
| 255 |
|
| 256 |
// PresenceCallback should be called with PresenceReply or error. |
| 257 |
type PresenceCallback func(PresenceReply, error) |
| 258 |
|
| 259 |
// PresenceHandler called when presence request received from client. |
| 260 |
type PresenceHandler func(PresenceEvent, PresenceCallback) |
| 261 |
|
| 262 |
// PresenceStatsEvent has channel operation called for. |
| 263 |
type PresenceStatsEvent struct { |
| 264 |
Channel string |
| 265 |
} |
| 266 |
|
| 267 |
// PresenceStatsReply contains fields determining the reaction on presence request. |
| 268 |
type PresenceStatsReply struct { |
| 269 |
Result *PresenceStatsResult |
| 270 |
} |
| 271 |
|
| 272 |
// PresenceStatsCallback should be called with PresenceStatsReply or error. |
| 273 |
type PresenceStatsCallback func(PresenceStatsReply, error) |
| 274 |
|
| 275 |
// PresenceStatsHandler must handle incoming command from client. |
| 276 |
type PresenceStatsHandler func(PresenceStatsEvent, PresenceStatsCallback) |
| 277 |
|
| 278 |
// HistoryEvent has channel operation called for. |
| 279 |
type HistoryEvent struct { |
| 280 |
Channel string |
| 281 |
Filter HistoryFilter |
| 282 |
} |
| 283 |
|
| 284 |
// HistoryReply contains fields determining the reaction on history request. |
| 285 |
type HistoryReply struct { |
| 286 |
Result *HistoryResult |
| 287 |
} |
| 288 |
|
| 289 |
// HistoryCallback should be called with HistoryReply or error. |
| 290 |
type HistoryCallback func(HistoryReply, error) |
| 291 |
|
| 292 |
// HistoryHandler must handle incoming command from client. |
| 293 |
type HistoryHandler func(HistoryEvent, HistoryCallback) |
| 294 |
|
| 295 |
// SurveyEvent with Op and Data of survey. |
| 296 |
type SurveyEvent struct { |
| 297 |
Op string |
| 298 |
Data []byte |
| 299 |
} |
| 300 |
|
| 301 |
// SurveyReply contains survey reply fields. |
| 302 |
type SurveyReply struct { |
| 303 |
Code uint32 |
| 304 |
Data []byte |
| 305 |
} |
| 306 |
|
| 307 |
// SurveyCallback should be called with SurveyReply as soon as survey completed. |
| 308 |
type SurveyCallback func(SurveyReply) |
| 309 |
|
| 310 |
// SurveyHandler allows to set survey handler function. |
| 311 |
type SurveyHandler func(SurveyEvent, SurveyCallback) |