-
Notifications
You must be signed in to change notification settings - Fork 3
/
message.go
64 lines (54 loc) · 1.39 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
64
package socket
// 客户端交互的消息体
type Message struct {
Event string `json:"event"` // 事件名称
Content any `json:"content"` // 消息内容
}
func NewMessage(event string, content any) *Message {
return &Message{
Event: event,
Content: content,
}
}
// 推送的消息
type SenderContent struct {
IsAck bool
broadcast bool // 是否广播消息
exclude []int64 // 排除的用户(预留)
receives []int64 // 推送的用户
message *Message // 消息体
}
func NewSenderContent() *SenderContent {
return &SenderContent{
broadcast: false,
exclude: make([]int64, 0),
receives: make([]int64, 0),
}
}
func (s *SenderContent) SetAck(value bool) *SenderContent {
s.IsAck = value
return s
}
// 设置广播推送
func (s *SenderContent) SetBroadcast(value bool) *SenderContent {
s.broadcast = value
return s
}
func (s *SenderContent) SetMessage(event string, content any) *SenderContent {
s.message = NewMessage(event, content)
return s
}
// 设置推送客户端
func (s *SenderContent) SetReceive(cid ...int64) *SenderContent {
s.receives = append(s.receives, cid...)
return s
}
// 设置广播推送中需要过滤的客户端
func (s *SenderContent) SetExclude(cid ...int64) *SenderContent {
s.exclude = append(s.exclude, cid...)
return s
}
// 判断是否是广播推送
func (s *SenderContent) IsBroadcast() bool {
return s.broadcast
}