-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgame.go
More file actions
41 lines (33 loc) · 768 Bytes
/
Copy pathgame.go
File metadata and controls
41 lines (33 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package csgsi
import(
"net/http"
"io"
)
// Game ...
type Game struct {
// Channel for game state data.
Channel chan State
}
// Returns a new Game object.
func New(size int) *Game{
return &Game{Channel: make(chan State, size)}
}
// Starts listening to address provided.
// If a POST request is received, it sends it through
// the Game.Channel channel.
func (gs *Game) Listen(addr string) error {
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
state := &State{}
if err := parseJson(req.Body, &state); err != nil {
io.WriteString(res, "bad")
}
gs.Channel <- *state
io.WriteString(res, "ok")
}
})
if err := http.ListenAndServe(addr, nil); err != nil {
return err
}
return nil
}