forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.go
187 lines (156 loc) · 3.73 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package utils
import (
"chat/globals"
"database/sql"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/gorilla/websocket"
"io"
"net/http"
"time"
)
type WebSocket struct {
Ctx *gin.Context
Conn *websocket.Conn
MaxTimeout time.Duration
Closed bool
}
var defaultMaxTimeout = 15 * time.Minute
func CheckUpgrader(c *gin.Context, strict bool) *websocket.Upgrader {
return &websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
if !strict {
return true
}
origin := c.Request.Header.Get("Origin")
if globals.OriginIsAllowed(origin) {
return true
}
return false
},
}
}
func NewWebsocket(c *gin.Context, strict bool) *WebSocket {
upgrader := CheckUpgrader(c, strict)
if conn, err := upgrader.Upgrade(c.Writer, c.Request, nil); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": false,
"message": "",
"reason": err.Error(),
})
return nil
} else {
instance := &WebSocket{
Ctx: c,
Conn: conn,
}
instance.Init()
return instance
}
}
func NewWebsocketClient(url string) *WebSocket {
if conn, _, err := websocket.DefaultDialer.Dial(url, nil); err != nil {
return nil
} else {
instance := &WebSocket{
Conn: conn,
}
instance.Init()
return instance
}
}
func (w *WebSocket) Init() {
w.Closed = false
w.Conn.SetCloseHandler(func(code int, text string) error {
w.Closed = true
return nil
})
w.Conn.SetPongHandler(func(appData string) error {
return w.Conn.SetReadDeadline(time.Now().Add(w.GetMaxTimeout()))
})
}
func (w *WebSocket) SetMaxTimeout(timeout time.Duration) {
w.MaxTimeout = timeout
}
func (w *WebSocket) GetMaxTimeout() time.Duration {
if w.MaxTimeout <= 0 {
return defaultMaxTimeout
}
return w.MaxTimeout
}
func (w *WebSocket) Read() (int, []byte, error) {
return w.Conn.ReadMessage()
}
func (w *WebSocket) Write(messageType int, data []byte) error {
return w.Conn.WriteMessage(messageType, data)
}
func (w *WebSocket) Close() error {
return w.Conn.Close()
}
func (w *WebSocket) DeferClose() {
if err := w.Close(); err != nil {
return
}
}
func (w *WebSocket) NextWriter(messageType int) (io.WriteCloser, error) {
return w.Conn.NextWriter(messageType)
}
func (w *WebSocket) ReadJSON(v interface{}) error {
return w.Conn.ReadJSON(v)
}
func (w *WebSocket) SendJSON(v interface{}) error {
return w.Conn.WriteJSON(v)
}
func (w *WebSocket) Send(v interface{}) bool {
return w.SendJSON(v) == nil
}
func (w *WebSocket) Receive(v interface{}) bool {
return w.ReadJSON(v) == nil
}
func (w *WebSocket) SendText(message string) bool {
return w.Write(websocket.TextMessage, []byte(message)) == nil
}
func (w *WebSocket) DecrRate(key string) bool {
cache := w.GetCache()
return DecrInt(cache, key, 1)
}
func (w *WebSocket) IncrRate(key string) bool {
cache := w.GetCache()
_, err := Incr(cache, key, 1)
return err == nil
}
func (w *WebSocket) IncrRateWithLimit(key string, limit int64, expiration int64) bool {
cache := w.GetCache()
state, err := IncrWithLimit(cache, key, 1, limit, expiration)
return state && err == nil
}
func (w *WebSocket) GetCtx() *gin.Context {
return w.Ctx
}
func (w *WebSocket) GetDB() *sql.DB {
return GetDBFromContext(w.Ctx)
}
func (w *WebSocket) GetCache() *redis.Client {
return GetCacheFromContext(w.Ctx)
}
func (w *WebSocket) GetConn() *websocket.Conn {
return w.Conn
}
func (w *WebSocket) IsClosed() bool {
return w.Closed
}
func ReadForm[T interface{}](w *WebSocket) (*T, error) {
// golang cannot use generic type in class-like struct
// except ping
_, message, err := w.Read()
if err != nil {
return nil, err
} else if string(message) == "{\"type\":\"ping\"}" {
return ReadForm[T](w)
}
form, err := Unmarshal[T](message)
if err != nil {
return nil, err
}
return &form, nil
}