Merge branch 'main' of https://github.com/vaporvee/multiplayer-game-test
This commit is contained in:
1
server/.env.example
Normal file
1
server/.env.example
Normal file
@@ -0,0 +1 @@
|
||||
PORT=""
|
3
server/.gitignore
vendored
Normal file
3
server/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.env
|
||||
cert.pem
|
||||
key.pem
|
@@ -1 +0,0 @@
|
||||
# multiplayer-game-test
|
@@ -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
|
||||
)
|
||||
|
@@ -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=
|
||||
|
@@ -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."))
|
||||
for {
|
||||
_, msg, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user