initial migration

This commit is contained in:
stryan 2021-10-01 14:24:31 -04:00
commit 972f840994
4 changed files with 93 additions and 0 deletions

9
go.mod Normal file
View File

@ -0,0 +1,9 @@
module git.saintnet.tech/tomecraft/tome_ws
go 1.16
require (
git.saintnet.tech/tomecraft/tome_lib v0.1.2 // indirect
git.saintnet.tech/tomecraft/tome_server v0.1.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
)

11
go.sum Normal file
View File

@ -0,0 +1,11 @@
git.saintnet.tech/tomecraft/tome_game v0.0.0-20211001174426-36013e0b8925 h1:Td26lWIzlTTMEjarvtk/+uEBRaSfXp7zLP5Ynnpovfk=
git.saintnet.tech/tomecraft/tome_game v0.0.0-20211001174426-36013e0b8925/go.mod h1:mwjKGgwOXGgviCbrtgk0yFRVyIwi8LQWELIOQfV2KlE=
git.saintnet.tech/tomecraft/tome_lib v0.1.1/go.mod h1:Jekqa9ojNDOrcO1aL0IWKuhCQSE5+MNHVcYtTWA6uko=
git.saintnet.tech/tomecraft/tome_lib v0.1.2 h1:S2BHgsWBGFv9fy+qBHuKXv0kcjT7VP/7gDmJHje/+RQ=
git.saintnet.tech/tomecraft/tome_lib v0.1.2/go.mod h1:Jekqa9ojNDOrcO1aL0IWKuhCQSE5+MNHVcYtTWA6uko=
git.saintnet.tech/tomecraft/tome_server v0.1.0 h1:tnwCCPai6+SOu/AzJa4ePuYyYrn/yrkHr3bCS0b/Dnc=
git.saintnet.tech/tomecraft/tome_server v0.1.0/go.mod h1:ciBGZtw0LVnGd/pZSYqiWRZJ9eGjswt62xbSrPM7G5Y=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=

11
main.go Normal file
View File

@ -0,0 +1,11 @@
package tome_ws
import (
coordinator "git.saintnet.tech/tomecraft/tome_server"
)
func main() {
c := coordinator.NewCoordinator()
c.Start()
Serve(c)
}

62
server.go Normal file
View File

@ -0,0 +1,62 @@
package tome_ws
import (
"log"
"net/http"
"git.saintnet.tech/tomecraft/tome_lib"
coordinator "git.saintnet.tech/tomecraft/tome_server"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{} // use default options
func Serve(c *coordinator.Coordinator) {
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(c, w, r)
})
log.Println("starting websocket")
err := http.ListenAndServe(":7636", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func serveWs(c *coordinator.Coordinator, w http.ResponseWriter, r *http.Request) {
// Upgrade our raw HTTP connection to a websocket based one
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
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 tome_lib.SessionCommand
err := conn.ReadJSON(&cmd)
if err != nil {
log.Println("Error during message reading:", err)
break
}
if cmd.Command != tome_lib.SessionCmdPoll {
log.Printf("Received: %s", cmd)
}
resp := c.Coordinate(&cmd)
if err != nil {
panic(err)
}
if cmd.Command != tome_lib.SessionCmdPoll {
log.Printf("sending: %v", resp.Result)
}
err = conn.WriteJSON(resp)
if err != nil {
log.Println("Error during message writing:", err)
break
}
if resp.Result == tome_lib.SessionRespLeft {
break
}
}
}