snengame/internal/coordinator/server.go
Steve eb7b405641 cards are now passed as PortableCard objects
client supports all actions
A bunch of stuff uses *Decks now instead of []Card since json marshaling
all of this code is absolutely awful
2021-07-23 16:43:39 -04:00

55 lines
1.1 KiB
Go

package coordinator
import (
"log"
"net/http"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{} // use default options
func Serve(c *Coordinator) {
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(c, w, r)
})
err := http.ListenAndServe(":7636", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func serveWs(c *Coordinator, w http.ResponseWriter, r *http.Request) {
// Upgrade our raw HTTP connection to a websocket based one
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("Error during connection upgradation:", err)
return
}
defer conn.Close()
// The event loop
for {
var cmd SessionCommand
err := conn.ReadJSON(&cmd)
if err != nil {
log.Println("Error during message reading:", err)
break
}
log.Printf("Received: %s", cmd)
resp := c.Coordinate(&cmd)
if err != nil {
panic(err)
}
log.Printf("sending: %v", resp)
err = conn.WriteJSON(resp)
if err != nil {
log.Println("Error during message writing:", err)
break
}
if resp.Result == SessionRespLeft {
break
}
}
}