move websocket server out of internal

This commit is contained in:
stryan 2021-07-25 16:32:41 -04:00
parent 58a853a241
commit 907cf269cd
2 changed files with 7 additions and 6 deletions

View File

@ -5,5 +5,5 @@ import "git.saintnet.tech/stryan/snengame/internal/coordinator"
func main() { func main() {
c := coordinator.NewCoordinator() c := coordinator.NewCoordinator()
c.Start() c.Start()
coordinator.Serve(c) Serve(c)
} }

View File

@ -1,15 +1,16 @@
package coordinator package main
import ( import (
"log" "log"
"net/http" "net/http"
"git.saintnet.tech/stryan/snengame/internal/coordinator"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
var upgrader = websocket.Upgrader{} // use default options var upgrader = websocket.Upgrader{} // use default options
func Serve(c *Coordinator) { func Serve(c *coordinator.Coordinator) {
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(c, w, r) serveWs(c, w, r)
}) })
@ -19,7 +20,7 @@ func Serve(c *Coordinator) {
} }
} }
func serveWs(c *Coordinator, w http.ResponseWriter, r *http.Request) { func serveWs(c *coordinator.Coordinator, w http.ResponseWriter, r *http.Request) {
// Upgrade our raw HTTP connection to a websocket based one // Upgrade our raw HTTP connection to a websocket based one
conn, err := upgrader.Upgrade(w, r, nil) conn, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
@ -30,7 +31,7 @@ func serveWs(c *Coordinator, w http.ResponseWriter, r *http.Request) {
// The event loop // The event loop
for { for {
var cmd SessionCommand var cmd coordinator.SessionCommand
err := conn.ReadJSON(&cmd) err := conn.ReadJSON(&cmd)
if err != nil { if err != nil {
log.Println("Error during message reading:", err) log.Println("Error during message reading:", err)
@ -47,7 +48,7 @@ func serveWs(c *Coordinator, w http.ResponseWriter, r *http.Request) {
log.Println("Error during message writing:", err) log.Println("Error during message writing:", err)
break break
} }
if resp.Result == SessionRespLeft { if resp.Result == coordinator.SessionRespLeft {
break break
} }
} }