This commit is contained in:
2024-04-17 02:27:23 +02:00
7 changed files with 39 additions and 50 deletions

1
server/.env.example Normal file
View File

@@ -0,0 +1 @@
PORT=""

3
server/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.env
cert.pem
key.pem

View File

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

View File

@@ -4,5 +4,6 @@ go 1.21.0
require ( require (
github.com/gorilla/websocket v1.5.1 // indirect github.com/gorilla/websocket v1.5.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect
golang.org/x/net v0.17.0 // indirect golang.org/x/net v0.17.0 // indirect
) )

View File

@@ -1,4 +1,6 @@
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= 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 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=

View File

@@ -2,10 +2,11 @@ package main
import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
"os"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/joho/godotenv"
) )
var upgrader = websocket.Upgrader{ var upgrader = websocket.Upgrader{
@@ -13,63 +14,45 @@ var upgrader = websocket.Upgrader{
WriteBufferSize: 1024, WriteBufferSize: 1024,
} }
// UserToken represents a user token in the database. var clients = make(map[*websocket.Conn]bool) // connected clients
type UserToken struct { var broadcast = make(chan []byte) // broadcast channel
UserID string
Token string 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. func handleConnections(w http.ResponseWriter, r *http.Request) {
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) conn, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
log.Println(err) fmt.Println(err)
return return
} }
defer conn.Close() defer conn.Close()
// Example client ID. Replace with your actual client ID. clients[conn] = true
clientID := "your_client_id_here"
// Redirect the user to the OAuth URL. for {
redirectToItchOAuth(conn, clientID) _, msg, err := conn.ReadMessage()
// 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 { if err != nil {
log.Println(err) delete(clients, conn)
break
}
broadcast <- msg
} }
} }
func main() { func handleMessages() {
http.HandleFunc("/ws", handler) for {
log.Fatal(http.ListenAndServe(":8080", nil)) msg := <-broadcast
for client := range clients {
if err := client.WriteMessage(websocket.TextMessage, msg); err != nil {
client.Close()
delete(clients, client)
}
}
}
} }