-
Notifications
You must be signed in to change notification settings - Fork 1
/
websocket.go
108 lines (89 loc) · 2.37 KB
/
websocket.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
package zoox
import (
"fmt"
"net/http"
"github.com/go-zoox/core-utils/regexp"
"github.com/go-zoox/core-utils/strings"
"github.com/go-zoox/headers"
"github.com/go-zoox/logger"
websocket "github.com/go-zoox/websocket/server"
)
const headerConnectionValueUpgrade = "upgrade"
const headerUpgradeValueWebSocket = "websocket"
// WebSocketOption ...
type WebSocketOption struct {
Server websocket.Server
Middlewares []HandlerFunc
}
// WebSocket defines the method to add websocket route
func (g *RouterGroup) WebSocket(path string, opts ...func(opt *WebSocketOption)) (websocket.Server, error) {
opt := &WebSocketOption{}
for _, o := range opts {
o(opt)
}
if opt.Server == nil {
server, err := websocket.New()
if err != nil {
return nil, err
}
opt.Server = server
}
// handleFunc := append(opt.Middlewares, func(ctx *Context) {
// ctx.Status(200)
// opt.Server.ServeHTTP(ctx.Writer, ctx.Request)
// })
// g.addRoute(http.MethodGet, path, handleFunc...)
logger.Info("[router] register: %8s %s", "WS", g.prefix+path)
matchPath := func(requestPath string) (ok bool) {
re := fmt.Sprintf("^%s$", g.prefix+path)
if strings.Contains(re, ":") {
re = strings.ReplaceAllFunc(re, ":\\w+", func(b []byte) []byte {
return []byte("\\w+")
})
} else if strings.Contains(re, "{") {
re = strings.ReplaceAllFunc(re, "{.*}", func(b []byte) []byte {
return []byte("\\w+")
})
}
return regexp.Match(re, requestPath)
}
g.Use(func(ctx *Context) {
// ignore@1: method != get
if ctx.Method != http.MethodGet {
ctx.Next()
return
}
// ignore@2: connection != Upgrade
connection := ctx.Header().Get(headers.Connection)
if connection == "" {
ctx.Next()
return
} else if strings.ToLower(connection) != headerConnectionValueUpgrade {
ctx.Next()
return
}
// ignore@3: upgrade != websocket
upgrade := ctx.Header().Get(headers.Upgrade)
if upgrade == "" {
ctx.Next()
return
} else if strings.ToLower(upgrade) != headerUpgradeValueWebSocket {
ctx.Next()
return
}
// ignore@4: path != path
if ok := matchPath(ctx.Path); !ok {
ctx.Next()
return
}
// @TODO rewrites handlers
// => ignore old handlers
// => only use websocket handlers
ctx.index = -1
ctx.handlers = append(opt.Middlewares, func(ctx *Context) {
opt.Server.ServeHTTP(ctx.Writer, ctx.Request)
})
ctx.Next()
})
return opt.Server, nil
}