-
Notifications
You must be signed in to change notification settings - Fork 1
/
pub.go
63 lines (57 loc) · 1.14 KB
/
pub.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
package main
import (
"encoding/json"
"log"
"net/http"
)
func (c *connection) sendJson(msg_json *[]byte) {
select {
case c.send <- []byte(*msg_json):
default:
dropConn(c)
}
}
func (c *connection) sendData(msg interface{}) {
msg_json, err := json.Marshal(msg)
if err != nil {
log.Printf(err.Error())
}
c.sendJson(&msg_json)
}
type PubMsg struct {
Op string `json:"op"`
Intent string `json:"intent"`
Obj string `json:"obj,omitempty"`
}
func sendPub(intent string, obj string) string {
log.Printf("intent: %+v %+v", intent, obj)
sub, ok := h.subs[intent]
if ok {
for l := range sub {
msg := PubMsg{"intent", intent, obj}
l.sendData(msg)
log.Printf("pub: %+v", msg)
}
}
return "{\"status\": \"ok\"}"
}
func pubHandler(c http.ResponseWriter, req *http.Request) {
req.ParseForm()
intent, ok := req.URL.Query()["intent"]
if !ok {
intent, ok = req.Form["intent"]
if !ok {
return
}
}
obj, ok := req.URL.Query()["obj"]
if !ok {
obj, ok = req.Form["obj"]
if !ok {
obj = []string{""}
}
}
msg_json := []byte(sendPub(intent[0], obj[0]))
c.Header().Set("Content-Type", "application/json")
c.Write(msg_json)
}