-
Notifications
You must be signed in to change notification settings - Fork 141
/
mqFilter.go
47 lines (41 loc) · 1.15 KB
/
mqFilter.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
package lhttp
import (
"log"
"strings"
)
//if client send message include subscribe/publish/unsubscribe header
//this filter work,use nats as a message queue client
type mqHeadFilter struct {
*HeadFilterBase
}
func (*mqHeadFilter) AfterRequestFilterHandle(ws *WsHandler) {
var value string
var channels []string
if value = ws.GetHeader(HEADER_KEY_SUBSCRIBE); value != "" {
channels = strings.Split(value, " ")
for _, c := range channels {
if conn,err:=mq.Subscribe(c, ws.subscribeCallback); nil == err{
ws.subscribe_nats_conn[c] = conn
}else{
log.Println("Subscribe Error",err)
}
// log.Print("subscribe channel: ", c)
}
}
if value = ws.GetHeader(HEADER_KEY_PUBLISH); value != "" {
channels = strings.Split(value, " ")
for _, c := range channels {
ws.setResponse()
ws.resp.serializeMessage()
mq.Publish(c, ws.resp.message)
// log.Print("publish channel: ", c, "message:", ws.resp.message)
}
}
if value = ws.GetHeader(HEADER_KEY_UNSUBSCRIBE); value != "" {
channels = strings.Split(value, " ")
for _, c := range channels {
mq.Unsubscribe(ws.subscribe_nats_conn[c])
log.Print("unsubscribe channel: ", c)
}
}
}