-
Notifications
You must be signed in to change notification settings - Fork 194
/
wrap.go
99 lines (88 loc) · 2.02 KB
/
wrap.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
package websocket
import (
"github.com/Allenxuxu/gev"
"github.com/Allenxuxu/gev/log"
"github.com/Allenxuxu/gev/plugins/websocket/ws"
"github.com/Allenxuxu/gev/plugins/websocket/ws/util"
"github.com/gobwas/pool/pbytes"
)
// WSHandler WebSocket Server 注册接口
type WSHandler interface {
OnConnect(c *gev.Connection)
OnMessage(c *gev.Connection, msg []byte) (ws.MessageType, []byte)
OnClose(c *gev.Connection)
}
// HandlerWrap gev Handler wrap
type HandlerWrap struct {
wsHandler WSHandler
Upgrade *ws.Upgrader
}
// NewHandlerWrap websocket handler wrap
func NewHandlerWrap(u *ws.Upgrader, wsHandler WSHandler) *HandlerWrap {
return &HandlerWrap{
wsHandler: wsHandler,
Upgrade: u,
}
}
// OnConnect wrap
func (s *HandlerWrap) OnConnect(c *gev.Connection) {
s.wsHandler.OnConnect(c)
}
// OnMessage wrap
func (s *HandlerWrap) OnMessage(c *gev.Connection, ctx interface{}, payload []byte) interface{} {
header, ok := ctx.(*ws.Header)
if !ok && len(payload) != 0 { // 升级协议 握手
return payload
}
if ok {
if header.OpCode.IsControl() {
var (
out []byte
err error
)
switch header.OpCode {
case ws.OpClose:
out, err = util.HandleClose(header, payload)
if err != nil {
log.Error(err)
}
_ = c.ShutdownWrite()
case ws.OpPing:
out, err = util.HandlePing(payload)
if err != nil {
log.Error(err)
}
case ws.OpPong:
out, err = util.HandlePong(payload)
if err != nil {
log.Error(err)
}
}
return out
}
messageType, out := s.wsHandler.OnMessage(c, payload)
if len(out) > 0 {
var frame *ws.Frame
switch messageType {
case ws.MessageBinary:
frame = ws.NewBinaryFrame(out)
case ws.MessageText:
frame = ws.NewTextFrame(out)
}
var err error
out, err = ws.FrameToBytes(frame)
if err != nil {
log.Error(err)
}
return out
}
}
return nil
}
// OnClose wrap
func (s *HandlerWrap) OnClose(c *gev.Connection) {
s.wsHandler.OnClose(c)
if bts, ok := c.Get(headerbufferKey); ok {
pbytes.Put(bts.([]byte))
}
}