-
Notifications
You must be signed in to change notification settings - Fork 3
/
typeChannel.go
98 lines (84 loc) · 2.31 KB
/
typeChannel.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
93
94
95
96
97
98
package sseKit
import (
"encoding/base64"
"fmt"
"github.com/richelieu-yang/chimera/v2/src/component/web/push/pushKit"
"github.com/richelieu-yang/chimera/v2/src/crypto/base64Kit"
"github.com/richelieu-yang/chimera/v2/src/time/timeKit"
"github.com/richelieu-yang/chimera/v2/src/urlKit"
"net/http"
"time"
)
type SseChannel struct {
pushKit.BaseChannel
w http.ResponseWriter
r *http.Request
msgType messageType
interval *timeKit.Interval
}
func (channel *SseChannel) Initialize() error {
channel.interval = timeKit.NewInterval(func(t time.Time) {
if err := channel.Push(pushKit.PongData); err != nil {
pushKit.GetDefaultLogger().WithError(err).Error("Fail to pong")
return
}
}, channel.PongInterval)
return nil
}
// Dispose 仅是释放资源,不会关闭通道(应当先关闭通道,再释放资源).
func (channel *SseChannel) Dispose() {
channel.interval.Stop()
channel.interval = nil
}
// Push (写锁)推送消息给客户端.
func (channel *SseChannel) Push(data []byte) error {
return channel.PushMessage(channel.msgType, data)
}
// PushMessage (写锁)推送消息给客户端.
func (channel *SseChannel) PushMessage(msgType messageType, data []byte) (err error) {
var str string
switch msgType {
case MessageTypeEncode:
str = string(data)
str = urlKit.EncodeURIComponent(str)
case MessageTypeBase64:
str = base64Kit.EncodeToString(data, base64Kit.WithEncoding(base64.StdEncoding))
case MessageTypeRaw:
fallthrough
default:
str = string(data)
}
event := &Event{
Data: str,
}
if channel.Closed {
return pushKit.ChannelClosedError
}
/* 写锁 */
channel.LockFunc(func() {
if channel.Closed {
err = pushKit.ChannelClosedError
return
}
err = event.Push(channel.w)
})
return err
}
// Close (写锁)后端主动关闭通道.
func (channel *SseChannel) Close(reason string) error {
closeInfo := fmt.Sprintf("Closed by backend with reason(%s)", reason)
channel.Listeners.BeforeClosedByBackend(channel, closeInfo)
if channel.SetClosed() {
channel.CloseCh <- closeInfo
}
return nil
}
func (channel *SseChannel) BindGroup(group string) {
pushKit.BindGroup(channel, group)
}
func (channel *SseChannel) BindUser(user string) {
pushKit.BindUser(channel, user)
}
func (channel *SseChannel) BindBsid(bsid string) {
pushKit.BindBsid(channel, bsid)
}