-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
63 lines (53 loc) · 1.62 KB
/
message.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
package main
import "natschat/utils/apierrs"
const (
MessageTypeAuthAck = "AUTHACK"
MessageTypeSub = "SUB"
MessageTypeUnSub = "UNSUB"
MessageTypeChat = "CHAT"
MessageTypeSubAck = "SUBACK"
MessageTypeUnSubAck = "UNSUBACK"
MessageTypeChatAck = "CHATACK"
MessageTypeValidationErr = "BAD"
MessageTypeForbiddenErr = "FORBIDDEN"
MessageTypeUnauthorizedErr = "UNAUTHORIZED"
MessageTypeServerErr = "ERR"
)
var (
TypeMap = map[string]func() interface{}{
MessageTypeSub: func() interface{} { return &SubscriptionMessage{} },
MessageTypeUnSub: func() interface{} { return &SubscriptionMessage{} },
MessageTypeChat: func() interface{} { return &ChatMessage{} },
}
)
type Message struct {
Type string `validate:"required" json:"type"`
Body interface{} `validate:"required" json:"body"`
}
func NewMessage(typ string, body interface{}) Message {
return Message{
Type: typ,
Body: body,
}
}
type SubscriptionMessage struct {
Channel string `validate:"required" json:"channel"`
}
type ChatMessage struct {
ID string `json:"id"`
ClientID string `validate:"required" json:"clientId"`
Channel string `validate:"required" json:"channel"`
Content string `validate:"required" json:"content"`
}
type ValidationErrorMessage struct {
OriginalMessage string `json:"originalMessage"`
Errors []apierrs.ValidationError `json:"errors"`
}
type ServerErrorMessage struct {
Message string `json:"message"`
}
func NewServerErrorMessage(message string) ServerErrorMessage {
return ServerErrorMessage{
Message: message,
}
}