move websocket server out of internal
This commit is contained in:
parent
58a853a241
commit
907cf269cd
@ -5,5 +5,5 @@ import "git.saintnet.tech/stryan/snengame/internal/coordinator"
|
||||
func main() {
|
||||
c := coordinator.NewCoordinator()
|
||||
c.Start()
|
||||
coordinator.Serve(c)
|
||||
Serve(c)
|
||||
}
|
||||
|
@ -1,15 +1,16 @@
|
||||
package coordinator
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.saintnet.tech/stryan/snengame/internal/coordinator"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
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) {
|
||||
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
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
@ -30,7 +31,7 @@ func serveWs(c *Coordinator, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// The event loop
|
||||
for {
|
||||
var cmd SessionCommand
|
||||
var cmd coordinator.SessionCommand
|
||||
err := conn.ReadJSON(&cmd)
|
||||
if err != nil {
|
||||
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)
|
||||
break
|
||||
}
|
||||
if resp.Result == SessionRespLeft {
|
||||
if resp.Result == coordinator.SessionRespLeft {
|
||||
break
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user