-
Notifications
You must be signed in to change notification settings - Fork 3
/
type_processor.go
146 lines (128 loc) · 3.78 KB
/
type_processor.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package wsKit
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/richelieu-yang/chimera/v3/src/component/web/httpKit"
"github.com/richelieu-yang/chimera/v3/src/component/web/push/pushKit"
"github.com/richelieu-yang/chimera/v3/src/concurrency/mutexKit"
"github.com/richelieu-yang/chimera/v3/src/core/errorKit"
"github.com/richelieu-yang/chimera/v3/src/core/strKit"
"github.com/sirupsen/logrus"
"net/http"
"time"
)
type wsProcessor struct {
pushKit.Processor
// upgrader 是并发安全的
upgrader *websocket.Upgrader
idGenerator func() (string, error)
listeners pushKit.Listeners
msgType *MessageType
pongInterval time.Duration
}
func (p *wsProcessor) ProcessWithGin(ctx *gin.Context) {
p.Process(ctx.Writer, ctx.Request)
}
func (p *wsProcessor) Process(w http.ResponseWriter, r *http.Request) {
PolyfillWebSocketRequest(r)
// 先判断是不是websocket请求
if !IsWebSocketUpgrade(r) {
failureInfo := "Not a websocket upgrade request"
p.listeners.OnFailure(w, r, failureInfo)
return
}
// Upgrade(升级为WebSocket协议)
conn, err := p.upgrader.Upgrade(w, r, w.Header())
if err != nil {
err = errorKit.Wrapf(err, "fail to upgrade")
p.listeners.OnFailure(w, r, err.Error())
return
}
// PS: 对于 Conn.Close() ,可以多次调用,不会panic,但从第二次关闭开始,返回非nil的error(可以直接忽略).
defer conn.Close()
channel, err := p.newChannel(r, conn, make(chan string, 1))
if err != nil {
err = errorKit.Wrapf(err, "fail to new channel")
p.listeners.OnFailure(w, r, err.Error())
return
}
if err := channel.Initialize(); err != nil {
err = errorKit.Wrapf(err, "fail to initialize channel")
p.listeners.OnFailure(w, r, err.Error())
return
}
p.listeners.OnHandshake(w, r, channel)
conn.SetCloseHandler(func(code int, text string) error {
closeInfo := fmt.Sprintf("code: %d, text: %s", code, text)
if channel.SetClosed() {
channel.GetCloseCh() <- closeInfo
}
// 默认的close handler
message := websocket.FormatCloseMessage(code, text)
_ = conn.WriteControl(websocket.CloseMessage, message, time.Now().Add(time.Second))
return nil
})
go func() {
// test
defer func() {
logrus.Info("[TEST] receive goroutine ends...")
}()
/* 接收WebSocket客户端发来的消息 */
for {
messageType, data, err := conn.ReadMessage()
if err != nil {
var closeInfo string
var closeErr *websocket.CloseError
if errors.As(err, &closeErr) {
closeInfo = fmt.Sprintf("code: %d, text: %s", closeErr.Code, closeErr.Text)
} else {
closeInfo = fmt.Sprintf("Fail to read message because of error(%s)", err.Error())
}
if channel.SetClosed() {
channel.GetCloseCh() <- closeInfo
}
break
}
p.listeners.OnMessage(channel, messageType, data)
}
}()
select {
case <-r.Context().Done():
if channel.SetClosed() {
p.listeners.OnClose(channel, "Context of request is done.")
}
case closeInfo := <-channel.GetCloseCh():
p.listeners.OnClose(channel, closeInfo)
}
}
func (p *wsProcessor) newChannel(r *http.Request, conn *websocket.Conn, closeCh chan string) (pushKit.Channel, error) {
id, err := p.idGenerator()
if err != nil {
return nil, errorKit.Wrapf(err, "fail to generate id")
}
if err := strKit.AssertNotEmpty(id, "id"); err != nil {
return nil, err
}
ip := httpKit.GetClientIP(r)
channel := &WsChannel{
BaseChannel: pushKit.BaseChannel{
RWMutex: mutexKit.RWMutex{},
CloseCh: closeCh,
ClientIP: ip,
Type: "WebSocket",
Id: id,
Bsid: "",
User: "",
Group: "",
Data: nil,
Closed: false,
Listeners: p.listeners,
PongInterval: p.pongInterval,
},
msgType: p.msgType,
conn: conn,
}
return channel, nil
}