112 lines
3.4 KiB
Go
112 lines
3.4 KiB
Go
package main
|
|
|
|
// These routines just recieve protobufs and pass protobufs back over
|
|
// a websocket (in this current case, a 'gorilla/websocket'
|
|
//
|
|
// These routines should probably be kept pretty generic in that regard
|
|
|
|
import "io"
|
|
import "log"
|
|
import "net/http"
|
|
|
|
import "github.com/golang/protobuf/proto"
|
|
import pb "git.wit.com/wit/witProtobuf"
|
|
import "github.com/gorilla/websocket"
|
|
|
|
// Also potentially look at:
|
|
// https://www.reddit.com/r/golang/comments/boznnz/an_alternative_to_gorilla_sessions_jeff_manage/
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
ReadBufferSize: 4096,
|
|
WriteBufferSize: 4096,
|
|
EnableCompression: true,
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
return true
|
|
},
|
|
}
|
|
|
|
func sendProtobuf(conn *websocket.Conn, msg *pb.Event) {
|
|
log.Println("sendProtobuf() START")
|
|
data, _ := proto.Marshal(msg)
|
|
err := conn.WriteMessage(websocket.BinaryMessage, data)
|
|
if err != nil {
|
|
log.Println("sendProtobuf() write:", err)
|
|
}
|
|
log.Println("sendProtobuf() END")
|
|
}
|
|
|
|
func eventHandler(w http.ResponseWriter, r *http.Request) {
|
|
log.Println("echoHandler() START")
|
|
log.Println("echoHandler() ATTEMPT r.Header.Get", r.Header.Get("User-Agent"))
|
|
log.Println("echoHandler() ATTEMPT r.Header", r.Header)
|
|
for key, value := range r.Header {
|
|
log.Println("echoHandler() HEADER key, value =", key, value)
|
|
}
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
if err != nil {
|
|
log.Println("Upgrade:", err)
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
|
|
for {
|
|
mytype, message, err := conn.ReadMessage()
|
|
if err != nil {
|
|
log.Println("read:", err)
|
|
return
|
|
}
|
|
|
|
if mytype == websocket.TextMessage {
|
|
// probably this should never happen since the client socket connection should have
|
|
// already been dropped. If a user is doing this, the account should probably be
|
|
// terminated as abuse
|
|
log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. NO HANDLER FOR THIS YET)")
|
|
log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. THIS SHOULD NEVER HAPPEN)")
|
|
log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. SUSPEND THIS USER)")
|
|
}
|
|
if mytype == websocket.BinaryMessage {
|
|
log.Println("\teventHandler() GOT websocket.BinaryMessage")
|
|
pdata := new(pb.Event)
|
|
var e *pb.Event
|
|
e = new(pb.Event)
|
|
err = proto.Unmarshal(message, pdata)
|
|
if (err != nil) {
|
|
log.Printf("readConn() something fucked up happened in Unmarshal")
|
|
}
|
|
log.Printf("\teventHandler() pdata.Type =", pdata.Type)
|
|
log.Printf("\teventHandler() recv binary: %s", pdata)
|
|
if pdata.Type == pb.Event_GET {
|
|
log.Printf("\teventHandler() DOING Event_GET")
|
|
e = processGetEvent(pdata)
|
|
} else if pdata.Type == pb.Event_LOGIN {
|
|
log.Printf("\teventHandler() DOING Event_LOGIN")
|
|
e = processLoginEvent(pdata)
|
|
} else if pdata.Type == pb.Event_ADD {
|
|
log.Printf("\teventHandler() DOING Event_ADD")
|
|
e = processAddEvent(pdata)
|
|
}
|
|
if (e == nil) {
|
|
e = pb.MakeFailResponse()
|
|
e.Comment = "websocket.BinaryMessage LOGIN FAILED"
|
|
}
|
|
e.Account = pdata.Account
|
|
log.Println("\teventHandler() e.Comment =", e.Comment)
|
|
sendProtobuf(conn, e)
|
|
}
|
|
}
|
|
}
|
|
|
|
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>")
|
|
}
|