-
Notifications
You must be signed in to change notification settings - Fork 3
/
session.go
60 lines (47 loc) · 1.08 KB
/
session.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
package socket
import (
"context"
"sync"
"time"
"golang.org/x/sync/errgroup"
)
// 客户端管理实例
var Session *session
var once sync.Once
// 渠道客户端
type session struct {
Chat *Channel // 默认分组
channels map[string]*Channel
// 可自行注册其它渠道...
}
func (s *session) Channel(name string) (*Channel, bool) {
val, ok := s.channels[name]
return val, ok
}
func Initialize(ctx context.Context, eg *errgroup.Group, fn func(name string)) {
once.Do(func() {
initialize(ctx, eg, fn)
})
}
func initialize(ctx context.Context, eg *errgroup.Group, fn func(name string)) {
Session = &session{
Chat: NewChannel("chat", make(chan *SenderContent, 5<<20)),
channels: map[string]*Channel{},
}
Session.channels["chat"] = Session.Chat
// 延时启动守护协程
time.AfterFunc(3*time.Second, func() {
eg.Go(func() error {
defer fn("health exit")
return health.Start(ctx)
})
eg.Go(func() error {
defer fn("ack exit")
return ack.Start(ctx)
})
eg.Go(func() error {
defer fn("chat exit")
return Session.Chat.Start(ctx)
})
})
}