2019-05-12 00:48:59 -05:00
|
|
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Command server is a test server for the Autobahn WebSockets Test Suite.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "flag"
|
|
|
|
import "io"
|
|
|
|
import "log"
|
|
|
|
import "net/http"
|
2019-05-12 00:57:19 -05:00
|
|
|
|
|
|
|
// import "time"
|
|
|
|
// import "errors"
|
|
|
|
// import "unicode/utf8"
|
2019-05-12 00:48:59 -05:00
|
|
|
|
|
|
|
import "github.com/gorilla/websocket"
|
|
|
|
|
|
|
|
import "github.com/golang/protobuf/proto"
|
|
|
|
import pb "git.wit.com/wit/witProtobuf"
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 4096,
|
|
|
|
WriteBufferSize: 4096,
|
|
|
|
EnableCompression: true,
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-12 00:57:19 -05:00
|
|
|
func eventHandler(w http.ResponseWriter, r *http.Request) {
|
2019-05-12 00:48:59 -05:00
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Upgrade:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2019-05-12 00:57:19 -05:00
|
|
|
log.Println("echoHandler() START")
|
2019-05-12 00:48:59 -05:00
|
|
|
|
|
|
|
for {
|
|
|
|
mytype, message, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("read:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if mytype == websocket.TextMessage {
|
2019-05-12 00:57:19 -05:00
|
|
|
log.Println("echoHandler() TextMessage mytype=", mytype)
|
2019-05-12 00:48:59 -05:00
|
|
|
}
|
|
|
|
if mytype == websocket.BinaryMessage {
|
2019-05-12 00:57:19 -05:00
|
|
|
log.Println("echoHandler() BinaryMessage mytype=", mytype)
|
2019-05-12 00:48:59 -05:00
|
|
|
pdata := new(pb.Event)
|
|
|
|
err = proto.Unmarshal(message, pdata)
|
|
|
|
if (err != nil) {
|
|
|
|
log.Printf("readConn() something fucked up happened in Unmarshal")
|
|
|
|
}
|
|
|
|
log.Printf("recv binary: %s", pdata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveHome(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path != "/" {
|
|
|
|
http.Error(w, "Not found.", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != "GET" {
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
io.WriteString(w, "<html><body>Echo Server</body></html>")
|
|
|
|
}
|
|
|
|
|
|
|
|
var addr = flag.String("addr", ":9000", "http service address")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
http.HandleFunc("/", serveHome)
|
2019-05-12 00:57:19 -05:00
|
|
|
http.HandleFunc("/event", eventHandler)
|
2019-05-12 00:48:59 -05:00
|
|
|
|
|
|
|
log.Println("Starting http.ListenAndServe() on port 9000")
|
|
|
|
err := http.ListenAndServe(*addr, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("ListenAndServe: ", err)
|
|
|
|
}
|
|
|
|
}
|