-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (69 loc) · 1.74 KB
/
main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/gorilla/websocket"
)
// Define the WebSocket upgrader
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// WebSocket handler
func wsHandler(w http.ResponseWriter, r *http.Request) {
openAIapiKey := r.URL.Query().Get("api_key")
if openAIapiKey == "" {
http.Error(w, "API key is required", http.StatusUnauthorized)
return
}
err := validateAPIKey(openAIapiKey)
if err != nil {
http.Error(w, "Invalid API key", http.StatusUnauthorized)
return
}
// Upgrade HTTP request to a WebSocket connection
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println("Failed to upgrade to WebSocket:", err)
return
}
fmt.Printf("A client has connected, starting game: %s\n", conn.RemoteAddr().String())
defer conn.Close()
// Start a game
game := NewGame(conn, openAIapiKey)
// Ping the client periodically to see if the connection is still alive
go func() {
for {
fmt.Printf("Pinging client\n")
err := conn.WriteMessage(websocket.PingMessage, nil)
if err != nil {
fmt.Println("Failed to ping client:", err)
game.End()
return
}
<-time.After(1 * time.Second)
}
}()
// If the client disconnects, close the connection
conn.SetCloseHandler(func(code int, text string) error {
fmt.Printf("Client disconnected: %d %s\n", code, text)
game.End()
return nil
})
RunGame(game)
}
func main() {
// WebSocket endpoint
http.HandleFunc("/ws", wsHandler)
port := os.Getenv("WEBSOCKET_PORT")
fmt.Printf("Server running on port %s\n", port)
err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
if err != nil {
fmt.Println("Server failed:", err)
}
}