-
Notifications
You must be signed in to change notification settings - Fork 141
/
wsHandler.go
289 lines (239 loc) · 6.15 KB
/
wsHandler.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package lhttp
import (
"container/list"
// "log"
"net/url"
"strings"
)
type WsMessage struct {
//message raw data
message string
//message command type
command string
//message headers
headers map[string]string
//message body
body string
}
//fill message by command headers and body
func (m *WsMessage) serializeMessage() string {
m.message = protocolNameWithVersion + " "
m.message += m.command + CRLF
for k, v := range m.headers {
m.message += k + ":" + v + CRLF
}
m.message += CRLF + m.body
return m.message
}
//parse websocket body
func buildMessage(data string) *WsMessage {
//TODO optimise ,to use builder pattern
s := data
message := &WsMessage{message: data}
message.headers = make(map[string]string, headerMax)
//parse message
//parse start line
i := strings.Index(s, CRLF)
message.command = s[protocolLength+1 : i]
//parse hearders
k := 0
headers := s[i+2:]
var key string
var value string
//traverse once
length := len(headers)
for j, ch := range headers {
if ch == ':' && key == "" {
key = headers[k:j]
k = j + 1
} else if length > j+1 && headers[j:j+2] == CRLF {
value = headers[k:j]
k = j + 2
message.headers[key] = value
// log.Print("parse head key:", key, " value:", value)
key = ""
}
if length > k+1 && headers[k:k+2] == CRLF {
k += 2
break
}
}
//set body
message.body = headers[k:]
return message
}
type WsHandler struct {
callbacks HandlerCallbacks
//websocket connection
conn *Conn
// nats conn
subscribe_nats_conn map[string]interface{}
//receive message
message *WsMessage
resp WsMessage
upstreamURL *url.URL
//one connection set id map sevel connections
//connSetID string
//save multipars datas, it is a list
multiparts *multipartBlock
}
func (req *WsHandler) reset() {
req.resp = WsMessage{command: "", headers: nil, body: ""}
}
func (req *WsHandler) GetMultipart() *multipartBlock {
return req.multiparts
}
//define subscribe callback as a WsHandler method is very very very importent
func (req *WsHandler) subscribeCallback(s string) {
Message.Send(req.conn, s)
}
func (req *WsHandler) SetCommand(s string) {
req.resp.command = s
}
func (req *WsHandler) GetCommand() string {
return req.message.command
}
func (req *WsHandler) SetHeader(hkey ,hvalue string) {
req.message.headers[hkey] = hvalue
}
func (req *WsHandler) GetHeader(hkey string) string {
if value, ok := req.message.headers[hkey]; ok {
return value
} else {
return ""
}
}
//if header already exist,update it
func (req *WsHandler) AddHeader(hkey, hvalue string) {
req.resp.headers = make(map[string]string, headerMax)
req.resp.headers[hkey] = hvalue
}
func (req *WsHandler) GetBody() string {
return req.message.body
}
//if response is nil, use request to fill it
func (req *WsHandler) setResponse() {
if req.resp.command == "" {
req.resp.command = req.message.command
}
if req.resp.headers == nil {
req.resp.headers = req.message.headers
}
if req.resp.body == "" {
req.resp.body = req.message.body
}
}
//if you want change command or header ,using SetCommand or AddHeader
func (req *WsHandler) Send(body string) {
resp := protocolNameWithVersion + " "
if req.resp.command != "" {
resp = resp + req.resp.command + CRLF
} else {
resp = resp + req.message.command + CRLF
}
if req.resp.headers != nil {
for k, v := range req.resp.headers {
resp = resp + k + ":" + v + CRLF
}
} else {
for k, v := range req.message.headers {
resp = resp + k + ":" + v + CRLF
}
}
resp += CRLF + body
req.resp.message = resp
// log.Print("send message:", string(req.message.message))
Message.Send(req.conn, req.resp.message)
req.resp = WsMessage{command: "", headers: nil, body: ""}
}
type HandlerCallbacks interface {
OnOpen(*WsHandler)
OnClose(*WsHandler)
OnMessage(*WsHandler)
}
type BaseProcessor struct {
}
func (*BaseProcessor) OnOpen(*WsHandler) {
// log.Print("base on open")
}
func (*BaseProcessor) OnMessage(*WsHandler) {
// log.Print("base on message")
}
func (*BaseProcessor) OnClose(*WsHandler) {
// log.Print("base on close")
}
func StartServer(ws *Conn) {
openFlag := 0
//init WsHandler,set connection and connsetid
wsHandler := &WsHandler{conn: ws}
wsHandler.subscribe_nats_conn = make(map[string]interface{}, subscribeMax)
for {
var data string
err := Message.Receive(ws, &data)
// log.Print("receive message:", string(data))
if err != nil {
break
}
l:=len(data)
if l <= protocolLength {
//TODO how to provide other protocol
// log.Print("TODO provide other protocol")
continue
}
l=len(data)
if l > MaxLength {
//TODO how to provide other protocol
// log.Print("TODO provide other protocol")
continue
}
if data[:protocolLength] != protocolNameWithVersion {
//TODO how to provide other protocol
// log.Print("TODO provide other protocol")
continue
}
wsHandler.message = buildMessage(data)
wsHandler.reset()
var e *list.Element
//head filter before process message
for e = beforeRequestFilterList.Front(); e != nil; e = e.Next() {
e.Value.(HeadFilterHandler).BeforeRequestFilterHandle(wsHandler)
}
wsHandler.callbacks = getProcessor(wsHandler.message.command)
if wsHandler.callbacks == nil {
wsHandler.callbacks = &BaseProcessor{}
}
// log.Print("callbacks:", wsHandler.callbacks.OnMessage)
//just call once
if openFlag == 0 {
for e = onOpenFilterList.Front(); e != nil; e = e.Next() {
e.Value.(HeadFilterHandler).OnOpenFilterHandle(wsHandler)
}
if wsHandler.callbacks != nil {
wsHandler.callbacks.OnOpen(wsHandler)
} else {
//log.Print("error on open is null")
}
openFlag = 1
}
if wsHandler.callbacks != nil {
wsHandler.callbacks.OnMessage(wsHandler)
} else {
//log.Print("error onmessage is null ")
}
//head filter after process message
for e = afterRequestFilterList.Front(); e != nil; e = e.Next() {
e.Value.(HeadFilterHandler).AfterRequestFilterHandle(wsHandler)
}
}
defer func() {
for e := onCloseFilterList.Front(); e != nil; e = e.Next() {
e.Value.(HeadFilterHandler).OnCloseFilterHandle(wsHandler)
}
if wsHandler.callbacks != nil {
wsHandler.callbacks.OnClose(wsHandler)
} else {
//log.Print("error on close is null")
}
ws.Close()
}()
}