-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
55 lines (46 loc) · 1.12 KB
/
server.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
55
package main
import (
"flag"
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
"github.com/smorken/freerange/freerangeserver"
)
var addr = flag.String("addr", "localhost:8080", "http service address")
var upgrader = websocket.Upgrader{} // use default options
func clientLoop(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
gameContext := freerangeserver.NewGameContext()
gameserver := freerangeserver.NewServer()
defer gameserver.CloseServer()
//err = c.WriteMessage(1, []byte("initial config"))
for {
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
response := gameserver.Reply(message)
err = c.WriteMessage(mt, response)
if err != nil {
log.Println("write:", err)
break
}
time.Sleep(16 * time.Millisecond)
}
}
func main() {
flag.Parse()
log.SetFlags(0)
fs := http.FileServer(http.Dir("frontend/"))
http.Handle("/", fs)
http.HandleFunc("/client", clientLoop)
log.Fatal(http.ListenAndServe(*addr, nil))
}