-
Notifications
You must be signed in to change notification settings - Fork 2
/
message.go
92 lines (71 loc) · 1.3 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package sugar
import (
"errors"
"time"
)
type Message struct {
// Contents
}
type Subscription struct {
ch chan Message
Inbox chan Message
}
func (s *Subscription) Publish(msg Message) error {
if _, ok := <-s.ch; !ok {
return errors.New("Topic has been closed")
}
s.ch <- msg
return nil
}
type Topic struct {
Subscribers []Session
MessageHistory []Message
ch <-chan Message
}
func (t *Topic) Subscribe(uid uint64, name string) (Subscription, error) {
// Get session and create one if it's the first
s := Session{
User: User{
ID: uid,
Name: name,
},
Timestamp: time.Now(),
}
// Add session to the Topic & MessageHistory
t.Subscribers = append(t.Subscribers, s)
// Create a subscription
return Subscription{}, nil
}
func (t *Topic) Unsubscribe(Subscription) error {
// Implementation
return nil
}
func (t *Topic) Delete() error {
// Implementation
return nil
}
type User struct {
ID uint64
Name string
}
type Session struct {
User User
Timestamp time.Time
}
var (
gSubscription Subscription
gTopic Topic
c = make(chan Message)
)
func work() {
gSubscription.ch = c
gSubscription.Publish(Message{})
}
func hub() {
for {
select {
case data := <-c:
gTopic.MessageHistory = append(gTopic.MessageHistory, data)
}
}
}