user ids, per-user logging

master
Jordan Orelli 5 years ago
parent f9cb5adfd8
commit bef1c6210a

@ -12,17 +12,18 @@ func main() {
stdout := blammo.NewLineWriter(os.Stdout) stdout := blammo.NewLineWriter(os.Stdout)
stderr := blammo.NewLineWriter(os.Stderr) stderr := blammo.NewLineWriter(os.Stderr)
log := blammo.NewLog("kloam", blammo.DebugWriter(stdout), blammo.InfoWriter(stdout), blammo.ErrorWriter(stderr)) log := blammo.NewLog("kloam", blammo.DebugWriter(stdout), blammo.InfoWriter(stdout), blammo.ErrorWriter(stderr))
log.Info("this is some info")
log.Info("some more info here")
s := server{ s := server{
Log: log, Log: log,
join: make(chan player),
} }
s.init() s.init()
go s.run()
lis, err := net.Listen("tcp", "0.0.0.0:9001") lis, err := net.Listen("tcp", "0.0.0.0:9001")
if err != nil { if err != nil {
log.Error("listen error: %v", err) log.Error("listen error: %v", err)
return return
} }
log.Info("listening on %v", lis.Addr())
http.Serve(lis, s.handler()) http.Serve(lis, s.handler())
} }

@ -1,10 +1,70 @@
package main package main
import "github.com/gorilla/websocket" import (
"context"
"io/ioutil"
"time"
"github.com/gorilla/websocket"
"github.com/jordanorelli/blammo"
)
type player struct { type player struct {
*blammo.Log
conn *websocket.Conn conn *websocket.Conn
id int
} }
func (p *player) readMessages() { func (p *player) run() {
ctx, cancel := context.WithCancel(context.Background())
p.readMessages(cancel)
go p.writeMessages(ctx)
p.conn.Close()
}
func (p *player) readMessages(cancel func()) {
defer cancel()
for {
_, rd, err := p.conn.NextReader()
if err != nil {
if closeError, ok := err.(*websocket.CloseError); ok {
switch closeError.Code {
case websocket.CloseNormalClosure:
p.Info("client disconnected: %s", closeError.Text)
default:
p.Error("client disconnected weirdly: %#v", *closeError)
}
} else {
p.Error("nextreader error: %s", err)
}
return
}
b, err := ioutil.ReadAll(rd)
if err != nil {
p.Error("read error: %s", err)
} else {
p.Child("rcv").Info(string(b))
}
}
}
func (p *player) writeMessages(ctx context.Context) {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
w, err := p.conn.NextWriter(websocket.TextMessage)
if err != nil {
}
w.Write([]byte("tick"))
w.Close()
case <-ctx.Done():
return
}
}
} }

@ -1,8 +1,8 @@
package main package main
import ( import (
"io/ioutil"
"net/http" "net/http"
"strconv"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@ -12,6 +12,8 @@ import (
type server struct { type server struct {
*blammo.Log *blammo.Log
router *mux.Router router *mux.Router
players map[int]*player
join chan player
} }
func (s *server) init() { func (s *server) init() {
@ -33,23 +35,27 @@ func (s *server) play(w http.ResponseWriter, r *http.Request) {
s.Error("upgrade error: %s", err) s.Error("upgrade error: %s", err)
return return
} }
defer conn.Close()
s.Info("client connected: %v", conn.RemoteAddr()) s.Info("client connected: %v", conn.RemoteAddr())
s.join <- player{
for { conn: conn,
_, rd, err := conn.NextReader()
if err != nil {
s.Error("nextreader error: %s", err)
break
} }
}
b, err := ioutil.ReadAll(rd) func (s *server) run() {
if err != nil { s.players = make(map[int]*player)
s.Error("read error: %s", err)
} else { for pc := 0; true; pc++ {
s.Child("rcv").Info(string(b)) s.step(pc)
}
} }
}
func (s *server) step(pc int) {
select {
case p := <-s.join:
s.Info("received join: %#v", p)
p.id = pc
p.Log = s.Child("players").Child(strconv.Itoa(p.id))
go p.run()
}
} }

Loading…
Cancel
Save