-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
55 lines (45 loc) · 1.04 KB
/
main.go
File metadata and controls
55 lines (45 loc) · 1.04 KB
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 (
"fmt"
"net/http"
"os"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
var lobby = newLobby()
func handleWebsocket(w http.ResponseWriter, r *http.Request) {
fmt.Println("Got connection", r.Host)
if r.Method != "GET" {
fmt.Println("ONLY GET")
http.Error(w, "ONLY GET", http.StatusMethodNotAllowed)
}
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println("Cannot upgrade", err)
return
}
client := fromWS(ws)
client.enterLobby(lobby)
}
func handleHttp(w http.ResponseWriter, r *http.Request) {
fmt.Println("Got keepalive")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(200)
w.Write([]byte("nice"))
}
func main() {
var port = os.Getenv("PORT")
if port == "" {
port = "1313"
}
http.HandleFunc("/keepalive", handleHttp)
http.HandleFunc("/", handleWebsocket)
fmt.Println("LISTENING", port)
fmt.Println(http.ListenAndServe("0.0.0.0:"+port, nil))
}