github.com/emitter-io/go/v2@v2.1.0/types.go (about) 1 // Copyright (c) Roman Atachiants and contributors. All rights reserved. 2 // Licensed under the MIT license. See LICENSE file in the project root for details. 3 4 package emitter 5 6 import ( 7 "crypto/rand" 8 "encoding/json" 9 "fmt" 10 ) 11 12 // MessageHandler is a callback type which can be set to be 13 // executed upon the arrival of messages published to topics 14 // to which the client is subscribed. 15 type MessageHandler func(*Client, Message) 16 17 // PresenceHandler is a callback type which can be set to be executed upon 18 // the arrival of presence events. 19 type PresenceHandler func(*Client, PresenceEvent) 20 21 // ErrorHandler is a callback type which can be set to be executed upon 22 // the arrival of an emitter error. 23 type ErrorHandler func(*Client, Error) 24 25 // DisconnectHandler is a callback type which can be set to be 26 // executed upon an unintended disconnection from the MQTT broker. 27 // Disconnects caused by calling Disconnect or ForceDisconnect will 28 // not cause an OnConnectionLost callback to execute. 29 type DisconnectHandler func(*Client, error) 30 31 // ConnectHandler is a callback that is called when the client 32 // state changes from unconnected/disconnected to connected. Both 33 // at initial connection and on reconnection 34 type ConnectHandler func(*Client) 35 36 // Option represents a key/value pair that can be supplied to the publish/subscribe or unsubscribe 37 // methods and provide ways to configure the operation. 38 type Option interface { 39 String() string 40 } 41 42 // Error represents an event code which provides a more details. 43 type Error struct { 44 Request uint16 `json:"req,omitempty"` 45 Status int `json:"status"` 46 Message string `json:"message"` 47 } 48 49 // Error returns the error message. 50 func (e *Error) Error() string { 51 return e.Message 52 } 53 54 // RequestID returns the request ID for the response. 55 func (e *Error) RequestID() uint16 { 56 return e.Request 57 } 58 59 // ------------------------------------------------------------------------------------ 60 61 // KeyGenRequest represents a request that can be sent to emitter broker 62 // in order to generate a new channel key. 63 type keygenRequest struct { 64 Key string `json:"key"` 65 Channel string `json:"channel"` 66 Type string `json:"type"` 67 TTL int `json:"ttl"` 68 } 69 70 // KeyGenResponse represents a response from emitter broker which contains 71 // the response to the key generation request. 72 type keyGenResponse struct { 73 Request uint16 `json:"req,omitempty"` 74 Status int `json:"status"` 75 Key string `json:"key"` 76 Channel string `json:"channel"` 77 ErrorMessage string `json:"message"` 78 } 79 80 // RequestID returns the request ID for the response. 81 func (r *keyGenResponse) RequestID() uint16 { 82 return r.Request 83 } 84 85 // ------------------------------------------------------------------------------------ 86 87 // PresenceRequest represents a request that can be sent to emitter broker 88 // in order to request presence information. 89 type presenceRequest struct { 90 Key string `json:"key"` 91 Channel string `json:"channel"` 92 Status bool `json:"status"` 93 Changes bool `json:"changes"` 94 } 95 96 // presenceMessage represents a presence message, for partial unmarshal 97 type presenceMessage struct { 98 Request uint16 `json:"req,omitempty"` 99 Event string `json:"event"` 100 Channel string `json:"channel"` 101 Time int `json:"time"` 102 Who json.RawMessage `json:"who"` 103 } 104 105 // PresenceEvent represents a response from emitter broker which contains 106 // presence state or a join/leave notification. 107 type PresenceEvent struct { 108 presenceMessage 109 Who []PresenceInfo 110 } 111 112 // RequestID returns the request ID for the response. 113 func (r *PresenceEvent) RequestID() uint16 { 114 return r.Request 115 } 116 117 // PresenceInfo represents a response from emitter broker which contains 118 // presence information. 119 type PresenceInfo struct { 120 ID string `json:"id"` 121 Username string `json:"username"` 122 } 123 124 // ------------------------------------------------------------------------------------ 125 126 // meResponse represents information about the client. 127 type meResponse struct { 128 Request uint16 `json:"req,omitempty"` // The corresponding request ID. 129 ID string `json:"id"` // The private ID of the connection. 130 Links map[string]string `json:"links,omitempty"` // The set of pre-defined channels. 131 } 132 133 // RequestID returns the request ID for the response. 134 func (r *meResponse) RequestID() uint16 { 135 return r.Request 136 } 137 138 // ------------------------------------------------------------------------------------ 139 140 // linkRequest represents a request to create a link. 141 type linkRequest struct { 142 Name string `json:"name"` // The name of the shortcut, max 2 characters. 143 Key string `json:"key"` // The key for the channel. 144 Channel string `json:"channel"` // The channel name for the shortcut. 145 Subscribe bool `json:"subscribe"` // Specifies whether the broker should auto-subscribe. 146 } 147 148 // Link represents a response for the link creation. 149 type Link struct { 150 Request uint16 `json:"req,omitempty"` 151 Name string `json:"name,omitempty"` // The name of the shortcut, max 2 characters. 152 Channel string `json:"channel,omitempty"` // The channel which was registered. 153 } 154 155 // RequestID returns the request ID for the response. 156 func (r *Link) RequestID() uint16 { 157 return r.Request 158 } 159 160 // ------------------------------------------------------------------------------------ 161 162 // KeyBanRequest represents a request that can be sent to emitter broker 163 // in order to ban/blacklist a channel key. 164 type keybanRequest struct { 165 Secret string `json:"secret"` // The master key to use. 166 Target string `json:"target"` // The target key to ban. 167 Banned bool `json:"banned"` // Whether the target should be banned or not. 168 } 169 170 // keyBanResponse represents a response from emitter broker which contains 171 // the response to the key ban request. 172 type keyBanResponse struct { 173 Request uint16 `json:"req,omitempty"` 174 Status int `json:"status"` // The status of the response 175 Banned bool `json:"banned"` // Whether the target should be banned or not. 176 ErrorMessage string `json:"message"` 177 } 178 179 // RequestID returns the request ID for the response. 180 func (r *keyBanResponse) RequestID() uint16 { 181 return r.Request 182 } 183 184 // ------------------------------------------------------------------------------------ 185 186 type MessageID []byte 187 188 type historyRequest struct { 189 Channel string `json:"channel"` 190 StartFromID MessageID `json:"startFromID"` 191 } 192 193 type HistoryMessage struct { 194 ID MessageID `json:"id"` 195 Topic string `json:"topic"` 196 Payload string `json:"payload"` 197 } 198 199 type historyResponse struct { 200 Request uint16 `json:"req,omitempty"` // The corresponding request ID. 201 Messages []HistoryMessage `json:"messages"` 202 } 203 204 // RequestID returns the request ID for the response. 205 func (r *historyResponse) RequestID() uint16 { 206 return r.Request 207 } 208 209 // ------------------------------------------------------------------------------------ 210 211 // uuid generates a simple UUID 212 func uuid() string { 213 b := make([]byte, 16) 214 _, err := rand.Read(b) 215 if err != nil { 216 panic(err) 217 } 218 219 return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]) 220 }