-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_conn.go
54 lines (44 loc) · 931 Bytes
/
ws_conn.go
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
package rogue
import (
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/websocket"
)
var (
upgrader = websocket.Upgrader{}
messageBufferSize = 8192
)
type messageResponse struct {
Type string
Message string
Time int64 // ticks since server reset
}
type moveResponse struct {
Type string
X float64
Y float64
LocalSpawns []LocalSpawn
}
type tickMessage struct {
Type string
FormattedTime string // XXX: remove this when js have int-to-time code
Time int64
}
type playerSpawnResponse struct {
moveResponse
Token string
}
func serveWebsocket(g *Game, w http.ResponseWriter, r *http.Request) {
socket, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Error("upgrade:", err)
return
}
client := &client{
socket: socket,
game: g,
send: make(chan []byte, messageBufferSize),
}
go client.write()
client.read()
}