got simple testing server working

This commit is contained in:
Stephen McQuay 2013-08-25 23:10:02 -07:00
parent ccd5dc3961
commit 880d73b54e
3 changed files with 40 additions and 11 deletions

14
game.go
View File

@ -19,6 +19,20 @@ type game struct {
sunregister chan *Spectator sunregister chan *Spectator
} }
func NewGame() *game {
return &game{
register: make(chan *player),
unregister: make(chan *player),
projectiles: make(map[*projectile]bool),
splosions: make(map[*splosion]bool),
players: make(map[*player]bool),
turn: 0,
spectators: make(map[*Spectator]bool),
sregister: make(chan *Spectator),
sunregister: make(chan *Spectator),
}
}
type Config struct { type Config struct {
ID string `json:"id"` ID string `json:"id"`
Stats stats `json:"stats"` Stats stats `json:"stats"`

13
main.go
View File

@ -19,17 +19,7 @@ var width = flag.Float64("width", 800, "width of field")
var height = flag.Float64("height", 550, "height of field") var height = flag.Float64("height", 550, "height of field")
var delta float64 var delta float64
var g = game{ var g *game
register: make(chan *player),
unregister: make(chan *player),
projectiles: make(map[*projectile]bool),
splosions: make(map[*splosion]bool),
players: make(map[*player]bool),
turn: 0,
spectators: make(map[*Spectator]bool),
sregister: make(chan *Spectator),
sunregister: make(chan *Spectator),
}
func main() { func main() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
@ -39,6 +29,7 @@ func main() {
http.Handle("/ws/", websocket.Handler(addPlayer)) http.Handle("/ws/", websocket.Handler(addPlayer))
g = NewGame()
go g.run() go g.run()
err := http.ListenAndServe(*addr, nil) err := http.ListenAndServe(*addr, nil)

View File

@ -1,7 +1,10 @@
package main package main
import ( import (
"code.google.com/p/go.net/websocket"
"encoding/json" "encoding/json"
"log"
"net/http"
"testing" "testing"
) )
@ -43,3 +46,24 @@ func TestClientIDParse(t *testing.T) {
t.Errorf("fail to parse: %v", err) t.Errorf("fail to parse: %v", err)
} }
} }
func TestProtocol(t *testing.T) {
http.Handle("/ws/", websocket.Handler(addPlayer))
g = NewGame()
go g.run()
go http.ListenAndServe(*addr, nil)
origin := "http://localhost/"
url := "ws://localhost:8666/ws/"
ws, err := websocket.Dial(url, "", origin)
if err != nil {
t.Errorf("fail: %v", err)
}
conf, err := Negociate(ws, "robot0", 800, 400)
if err != nil {
t.Errorf("fail: %v", err)
}
log.Printf("%+v", conf)
}