-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo.go
43 lines (33 loc) · 724 Bytes
/
echo.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
package main
import (
"log"
"net/http"
"github.com/xlionet/monkey"
)
type Echo struct {
monkey.WSProtocol
}
// OnTransportData ...
func (p *Echo) OnTransportData(transport monkey.Transport, env *monkey.Envelope) {
if env == nil {
log.Fatal("nil msg")
}
transport.SendData(env)
}
func main() {
protocol := &Echo{}
cfg := monkey.NewConfig()
cfg.WSListernPort = 8080
mk := monkey.New(protocol, cfg)
http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
err := mk.HandleConnection(w, r)
if err != nil {
log.Fatal(err)
}
})
http.HandleFunc("/", index)
mk.Serve(http.DefaultServeMux)
}
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "echo.html")
}