parse messages from client

master
Jordan Orelli 5 years ago
parent 30495c8302
commit 230702f7a3

@ -16,6 +16,7 @@ func main() {
s := server{ s := server{
Log: log, Log: log,
join: make(chan player), join: make(chan player),
inbox: make(chan message),
} }
s.init() s.init()
go s.run() go s.run()

@ -0,0 +1,6 @@
package main
type message struct {
from int
text string
}

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"fmt"
"io/ioutil" "io/ioutil"
"time" "time"
@ -12,14 +13,17 @@ import (
type player struct { type player struct {
*blammo.Log *blammo.Log
conn *websocket.Conn conn *websocket.Conn
server *server
id int id int
} }
func (p *player) run() { func (p *player) run() {
p.Info("starting run cycle")
defer p.Info("run cycle done")
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
p.readMessages(cancel)
go p.writeMessages(ctx) go p.writeMessages(ctx)
p.readMessages(cancel)
p.conn.Close() p.conn.Close()
} }
@ -47,22 +51,37 @@ func (p *player) readMessages(cancel func()) {
} else { } else {
p.Child("rcv").Info(string(b)) p.Child("rcv").Info(string(b))
} }
p.server.inbox <- message{
from: p.id,
text: string(b),
}
} }
} }
func (p *player) writeMessages(ctx context.Context) { func (p *player) writeMessages(ctx context.Context) {
p.Info("writeMessages loop start")
defer p.Info("writeMessage loop end")
ticker := time.NewTicker(1 * time.Second) ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop() n := 0
for { for {
select { select {
case <-ticker.C: case t := <-ticker.C:
n++
p.Info("trying to write a tick")
w, err := p.conn.NextWriter(websocket.TextMessage) w, err := p.conn.NextWriter(websocket.TextMessage)
if err != nil { if err != nil {
p.Error("error getting writer: %v", err)
return
}
fmt.Fprintf(w, "%d tick: %v", n, t)
if err := w.Close(); err != nil {
p.Error("close frame error: %v", err)
return
} }
w.Write([]byte("tick")) p.Info("wrote a tick")
w.Close()
case <-ctx.Done(): case <-ctx.Done():
return return
} }

@ -0,0 +1,58 @@
package main
import (
"encoding/json"
)
type request struct {
Cmd string `json:"cmd"`
}
func (r request) parse(msg string) command {
var c command
switch r.Cmd {
case "login":
c = new(login)
case "death":
c = new(death)
case "collect-soul":
c = new(collectSoul)
default:
return nil
}
json.Unmarshal([]byte(msg), &c)
return c
}
type vector3 struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Z float64 `json:"z"`
}
type command interface {
exec(*server)
}
type collectSoul struct {
PlayerName string `json:"playerName"`
Position vector3 `json:"position"`
}
func (c *collectSoul) exec(s *server) {
}
type login struct {
Username string `json:"username"`
Password string `json:"password"`
}
func (l *login) exec(s *server) {
}
type death struct {
Position vector3 `json:"position"`
}
func (d *death) exec(s *server) {
}

@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/json"
"net/http" "net/http"
"strconv" "strconv"
@ -14,6 +15,7 @@ type server struct {
router *mux.Router router *mux.Router
players map[int]*player players map[int]*player
join chan player join chan player
inbox chan message
} }
func (s *server) init() { func (s *server) init() {
@ -39,6 +41,7 @@ func (s *server) play(w http.ResponseWriter, r *http.Request) {
s.Info("client connected: %v", conn.RemoteAddr()) s.Info("client connected: %v", conn.RemoteAddr())
s.join <- player{ s.join <- player{
conn: conn, conn: conn,
server: s,
} }
} }
@ -57,5 +60,14 @@ func (s *server) step(pc int) {
p.id = pc p.id = pc
p.Log = s.Child("players").Child(strconv.Itoa(p.id)) p.Log = s.Child("players").Child(strconv.Itoa(p.id))
go p.run() go p.run()
case m := <-s.inbox:
s.Info("received message: %v", m)
var req request
if err := json.Unmarshal([]byte(m.text), &req); err != nil {
s.Error("dunno how to read this message: %v", m.text)
return
}
cmd := req.parse(m.text)
s.Info("cmd: %#v", cmd)
} }
} }

Loading…
Cancel
Save