-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
57 lines (44 loc) · 1.11 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
56
57
package main
import (
"flag"
"fmt"
"log"
"net/http"
"github.com/aarthikrao/jumpAndShoot/objects"
"github.com/gorilla/websocket"
)
var addr = flag.String("addr", "0.0.0.0:4000", "http service address")
func main() {
flag.Parse()
http.HandleFunc("/gameEngine", handler)
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "JumpAndShoot engine is running on this port")
})
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 2048,
WriteBufferSize: 2048,
CheckOrigin: func(r *http.Request) bool {
return true
},
EnableCompression: true,
}
func handler(w http.ResponseWriter, r *http.Request) {
playerName := r.URL.Query()["id"][0]
log.Println("PlayerJoined :", playerName)
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("ERROR", err)
return
}
// Create a new player instance
p := objects.NewPlayer(playerName, conn)
// Start listening to messages from player
go p.RecieveMessages()
// Find a match for the player
objects.GS.Match(p)
}