2021-10-27 18:56:17 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
// List of possible events
|
|
|
|
const (
|
|
|
|
openEvent = "open"
|
|
|
|
keepaliveEvent = "keepalive"
|
2021-10-29 12:29:27 +00:00
|
|
|
messageEvent = "message"
|
2021-10-27 18:56:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// message represents a message published to a topic
|
|
|
|
type message struct {
|
|
|
|
Time int64 `json:"time"` // Unix time in seconds
|
|
|
|
Event string `json:"event,omitempty"` // One of the above
|
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// messageEncoder is a function that knows how to encode a message
|
|
|
|
type messageEncoder func(msg *message) (string, error)
|
|
|
|
|
|
|
|
// newMessage creates a new message with the current timestamp
|
|
|
|
func newMessage(event string, msg string) *message {
|
|
|
|
return &message{
|
|
|
|
Time: time.Now().Unix(),
|
|
|
|
Event: event,
|
|
|
|
Message: msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newOpenMessage is a convenience method to create an open message
|
|
|
|
func newOpenMessage() *message {
|
|
|
|
return newMessage(openEvent, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// newKeepaliveMessage is a convenience method to create a keepalive message
|
|
|
|
func newKeepaliveMessage() *message {
|
|
|
|
return newMessage(keepaliveEvent, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// newDefaultMessage is a convenience method to create a notification message
|
|
|
|
func newDefaultMessage(msg string) *message {
|
2021-10-29 12:29:27 +00:00
|
|
|
return newMessage(messageEvent, msg)
|
2021-10-27 18:56:17 +00:00
|
|
|
}
|