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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
package game
import (
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
)
const (
writeWait = 10 * time.Second
pongWait = 60 * time.Second
pingPeriod = (pongWait * 9) / 10
maxMessageSize = 512
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
// TODO: insecure!
return true
},
}
func servePlayerWs(hub *gameHub, w http.ResponseWriter, r *http.Request, playerID int) error {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return err
}
player := &playerClient{
hub: hub,
conn: conn,
s2cMessages: make(chan playerMessageS2C),
playerID: playerID,
}
hub.registerPlayer <- player
go func() {
err := player.writePump()
log.Printf("%v", err)
}()
go func() {
err := player.readPump()
log.Printf("%v", err)
}()
return nil
}
func serveWatcherWs(hub *gameHub, w http.ResponseWriter, r *http.Request) error {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return err
}
watcher := &watcherClient{
hub: hub,
conn: conn,
s2cMessages: make(chan watcherMessageS2C),
}
hub.registerWatcher <- watcher
go func() {
err := watcher.writePump()
log.Printf("%v", err)
}()
go func() {
err := watcher.readPump()
log.Printf("%v", err)
}()
return nil
}
|