-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.go
72 lines (57 loc) · 1.38 KB
/
consumer.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
65
66
67
68
69
70
71
72
package socketconsumer
type ConsumerInterface interface {
Connect() bool
Accept()
Disconnect()
Receive(message *Message)
Send(message *Message)
GetId() string
}
type Consumer struct {
// Unique id of the consumer
Id string
hub *Hub
}
func NewConsumer(hub *Hub) *Consumer {
return &Consumer{
Id: RandomString(10),
hub: hub,
}
}
func (c *Consumer) GetId() string {
return c.Id
}
// Connect function for accept or reject the connection. used to authenticate the connection
func (c *Consumer) Connect() bool {
return true
}
// Disconnect function for disconnect task
func (c *Consumer) Disconnect() {}
// Accept function for accept the connection
func (c *Consumer) Accept() {}
// receive message
func (c *Consumer) Receive(message *Message) {}
// Send message
func (c *Consumer) Send(message *Message) {
c.hub.Send(c.Id, message)
}
// Send message to other consumer
func (c *Consumer) SendTo(id string, message *Message) {
c.hub.Send(id, message)
}
// Add group
func (c *Consumer) GroupAdd(name string) {
c.hub.GroupAdd(name, c)
}
// Discard group
func (c *Consumer) GroupDiscard(name string) {
c.hub.GroupDiscard(name, c)
}
// Send to group
func (c *Consumer) GroupSend(name string, message *Message) {
c.hub.GroupSend(name, message)
}
// Send to group except self
func (c *Consumer) GroupSendOthers(name string, message *Message) {
c.hub.GroupSendExcept(name, message, c)
}