diff --git a/project/.gitignore b/project/.gitignore index 4709183..68462ed 100644 --- a/project/.gitignore +++ b/project/.gitignore @@ -1,2 +1,2 @@ # Godot 4+ specific ignores -.godot/ +.godot/ \ No newline at end of file diff --git a/server/.env.example b/server/.env.example new file mode 100644 index 0000000..2f304be --- /dev/null +++ b/server/.env.example @@ -0,0 +1 @@ +PORT="" \ No newline at end of file diff --git a/server/.gitignore b/server/.gitignore new file mode 100644 index 0000000..f43abb9 --- /dev/null +++ b/server/.gitignore @@ -0,0 +1,3 @@ +.env +cert.pem +key.pem \ No newline at end of file diff --git a/server/README.md b/server/README.md deleted file mode 100644 index 1a662c1..0000000 --- a/server/README.md +++ /dev/null @@ -1 +0,0 @@ -# multiplayer-game-test diff --git a/server/go.mod b/server/go.mod index 7679732..ec730c8 100644 --- a/server/go.mod +++ b/server/go.mod @@ -4,5 +4,6 @@ go 1.21.0 require ( github.com/gorilla/websocket v1.5.1 // indirect + github.com/joho/godotenv v1.5.1 // indirect golang.org/x/net v0.17.0 // indirect ) diff --git a/server/go.sum b/server/go.sum index 272772f..737d760 100644 --- a/server/go.sum +++ b/server/go.sum @@ -1,4 +1,6 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= diff --git a/server/main.go b/server/main.go index bff705d..ae9cf6c 100644 --- a/server/main.go +++ b/server/main.go @@ -2,10 +2,11 @@ package main import ( "fmt" - "log" "net/http" + "os" "github.com/gorilla/websocket" + "github.com/joho/godotenv" ) var upgrader = websocket.Upgrader{ @@ -13,63 +14,45 @@ var upgrader = websocket.Upgrader{ WriteBufferSize: 1024, } -// UserToken represents a user token in the database. -type UserToken struct { - UserID string - Token string +var clients = make(map[*websocket.Conn]bool) // connected clients +var broadcast = make(chan []byte) // broadcast channel + +func main() { + godotenv.Load() + http.HandleFunc("/ws", handleConnections) + go handleMessages() + fmt.Println("Server running on " + os.Getenv("PORT")) + http.ListenAndServeTLS(os.Getenv("PORT"), "cert.pem", "key.pem", nil) } -// userTokens is a simple in-memory map for demonstration purposes. -var userTokens = make(map[string]UserToken) - -// generateItchOAuthURL generates an OAuth URL for itch.io. -func generateItchOAuthURL(clientID string) string { - return fmt.Sprintf("https://itch.io/user/oauth?client_id=%s&response_type=token", clientID) -} - -// redirectToItchOAuth sends the OAuth URL to the client. -func redirectToItchOAuth(conn *websocket.Conn, clientID string) { - oauthURL := generateItchOAuthURL(clientID) - err := conn.WriteMessage(websocket.TextMessage, []byte(oauthURL)) - if err != nil { - log.Println(err) - } -} - -// saveUserToken saves the user token in the database. -func saveUserToken(userID, token string) { - userTokens[userID] = UserToken{UserID: userID, Token: token} -} - -// handler handles WebSocket connections. -func handler(w http.ResponseWriter, r *http.Request) { +func handleConnections(w http.ResponseWriter, r *http.Request) { conn, err := upgrader.Upgrade(w, r, nil) if err != nil { - log.Println(err) + fmt.Println(err) return } defer conn.Close() - // Example client ID. Replace with your actual client ID. - clientID := "your_client_id_here" + clients[conn] = true - // Redirect the user to the OAuth URL. - redirectToItchOAuth(conn, clientID) - - // Simulate receiving the token from the client. - // In a real application, you would parse the token from the redirect URL. - userID := "example_user_id" - token := "example_token" - saveUserToken(userID, token) - - // Send a confirmation message to the client. - err = conn.WriteMessage(websocket.TextMessage, []byte("Token saved successfully.")) - if err != nil { - log.Println(err) + for { + _, msg, err := conn.ReadMessage() + if err != nil { + delete(clients, conn) + break + } + broadcast <- msg } } -func main() { - http.HandleFunc("/ws", handler) - log.Fatal(http.ListenAndServe(":8080", nil)) +func handleMessages() { + for { + msg := <-broadcast + for client := range clients { + if err := client.WriteMessage(websocket.TextMessage, msg); err != nil { + client.Close() + delete(clients, client) + } + } + } }