This commit is contained in:
2024-04-16 22:44:17 +02:00
parent d7f21a7727
commit e6be491e6e
9 changed files with 145 additions and 0 deletions

1
server/README.md Normal file
View File

@@ -0,0 +1 @@
# multiplayer-game-test

8
server/go.mod Normal file
View File

@@ -0,0 +1,8 @@
module github.com/vaporvee/multiplayer-game-test
go 1.21.0
require (
github.com/gorilla/websocket v1.5.1 // indirect
golang.org/x/net v0.17.0 // indirect
)

4
server/go.sum Normal file
View File

@@ -0,0 +1,4 @@
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=

75
server/main.go Normal file
View File

@@ -0,0 +1,75 @@
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
// UserToken represents a user token in the database.
type UserToken struct {
UserID string
Token string
}
// 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) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
defer conn.Close()
// Example client ID. Replace with your actual client ID.
clientID := "your_client_id_here"
// 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)
}
}
func main() {
http.HandleFunc("/ws", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}