forked from botuniverse/go-libonebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onebot_event.go
59 lines (51 loc) · 1.43 KB
/
onebot_event.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
package libonebot
import (
"encoding/json"
)
// Push 向与 OneBot 实例连接的接受端推送一个事件.
func (ob *OneBot) Push(event AnyEvent) bool {
if event == nil {
ob.Logger.Warnf("事件为空")
return false
}
if !event.tryFixUp(ob.Platform) {
ob.Logger.Warnf("事件字段值无效")
return false
}
ob.Logger.Debugf("事件: %#v", event)
eventJSONBytes, err := json.Marshal(event)
if err != nil {
ob.Logger.Warnf("事件序列化失败, 错误: %v", err)
return false
}
ob.Logger.Infof("事件 `%v` 开始推送", event.Name())
ob.eventListenChansLock.RLock() // use read lock to allow emitting events concurrently
defer ob.eventListenChansLock.RUnlock()
for _, ch := range ob.eventListenChans {
ch <- marshaledEvent{event.Name(), eventJSONBytes, event}
}
return true
}
type marshaledEvent struct {
name string
bytes []byte
raw AnyEvent
}
func (ob *OneBot) openEventListenChan() <-chan marshaledEvent {
ch := make(chan marshaledEvent) // TODO: channel size
ob.eventListenChansLock.Lock()
ob.eventListenChans = append(ob.eventListenChans, ch)
ob.eventListenChansLock.Unlock()
return ch
}
func (ob *OneBot) closeEventListenChan(ch <-chan marshaledEvent) {
ob.eventListenChansLock.Lock()
defer ob.eventListenChansLock.Unlock()
for i, c := range ob.eventListenChans {
if c == ch {
close(c)
ob.eventListenChans = append(ob.eventListenChans[:i], ob.eventListenChans[i+1:]...)
return
}
}
}