/
test.go
58 lines (48 loc) · 1.3 KB
/
test.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
package main
import (
"fmt"
"log"
"net/http"
"github.com/fanux/lhttp"
)
//ChatProcessor is
type ChatProcessor struct {
*lhttp.BaseProcessor
}
//OnMessage is
func (p *ChatProcessor) OnMessage(h *lhttp.WsHandler) {
log.Print("on OnMessage: ", h.GetBody())
h.AddHeader("content-type", "image/png")
h.SetCommand("auth")
h.Send(h.GetBody())
}
//SubPubProcessor is
type SubPubProcessor struct {
*lhttp.BaseProcessor
}
//UpstreamProcessor is
type UpstreamProcessor struct {
*lhttp.BaseProcessor
}
//UploadProcessor is
type UploadProcessor struct {
*lhttp.BaseProcessor
}
//OnMessage is
func (*UploadProcessor) OnMessage(ws *lhttp.WsHandler) {
for m := ws.GetMultipart(); m != nil; m = m.GetNext() {
log.Print("multibody:", m.GetBody(), " headers:", m.GetHeaders())
}
}
func main() {
lhttp.Regist("chat", &ChatProcessor{&lhttp.BaseProcessor{}})
lhttp.Regist("subpub", &SubPubProcessor{&lhttp.BaseProcessor{}})
lhttp.Regist("upstream", &UpstreamProcessor{&lhttp.BaseProcessor{}})
lhttp.Regist("upload", &UploadProcessor{&lhttp.BaseProcessor{}})
http.Handle("/echo", lhttp.Handler(lhttp.StartServer))
http.Handle("/", lhttp.Handler(lhttp.StartServer))
http.HandleFunc("/https", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", "world")
})
http.ListenAndServe(":8581", nil)
}