Merge branch 'main' of https://github.com/vaporvee/multiplayer-game-test
This commit is contained in:
111
server/main.go
111
server/main.go
@@ -1,58 +1,117 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"io"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var upgrader = websocket.Upgrader{
|
type Payload struct {
|
||||||
ReadBufferSize: 1024,
|
Type string `json:"type"`
|
||||||
WriteBufferSize: 1024,
|
Message string `json:"msg,omitempty"`
|
||||||
|
Direction *struct {
|
||||||
|
X int `json:"x,omitempty"`
|
||||||
|
Y int `json:"y,omitempty"`
|
||||||
|
} `json:"direction,omitempty"`
|
||||||
|
SessionID string `json:"session_id,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var clients = make(map[*websocket.Conn]bool) // connected clients
|
// Generate a unique session ID
|
||||||
var broadcast = make(chan []byte) // broadcast channel
|
func generateSessionID() string {
|
||||||
|
b := make([]byte, 32)
|
||||||
|
if _, err := io.ReadFull(rand.Reader, b); err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return base64.URLEncoding.EncodeToString(b)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
godotenv.Load()
|
godotenv.Load()
|
||||||
http.HandleFunc("/ws", handleConnections)
|
port := os.Getenv("PORT")
|
||||||
go handleMessages()
|
if port == "" {
|
||||||
fmt.Println("Server running on " + os.Getenv("PORT"))
|
port = "4477" // Default port if not specified
|
||||||
http.ListenAndServeTLS(os.Getenv("PORT"), "cert.pem", "key.pem", nil)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func handleConnections(w http.ResponseWriter, r *http.Request) {
|
addr, err := net.ResolveUDPAddr("udp", ":"+port)
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := net.ListenUDP("udp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
clients[conn] = true
|
fmt.Println("Server running on " + port)
|
||||||
|
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
clients := make(map[string]string) // Map to keep track of clients and their session IDs
|
||||||
|
|
||||||
for {
|
for {
|
||||||
_, msg, err := conn.ReadMessage()
|
n, addr, err := conn.ReadFromUDP(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
delete(clients, conn)
|
fmt.Println(err)
|
||||||
break
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var payload Payload
|
||||||
|
err = json.Unmarshal(buf[:n], &payload)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error parsing JSON:", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the client's IP and port as the key to uniquely identify the connection
|
||||||
|
key := addr.String()
|
||||||
|
|
||||||
|
// Check if the client's connection already has a session ID
|
||||||
|
if _, ok := clients[key]; !ok {
|
||||||
|
// The client is new, generate a session ID
|
||||||
|
sessionID := generateSessionID()
|
||||||
|
clients[key] = sessionID
|
||||||
|
// Send the session ID back to the client
|
||||||
|
responsePayload := Payload{SessionID: sessionID}
|
||||||
|
response, _ := json.Marshal(responsePayload)
|
||||||
|
conn.WriteToUDP(response, addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch payload.Type {
|
||||||
|
case "init":
|
||||||
|
fmt.Printf("Received initiation generated SessionID: %s", clients[key])
|
||||||
|
case "move":
|
||||||
|
fmt.Printf("Received move message from Session ID: %s : X=%d, Y=%d\n", clients[key], payload.Direction.X, payload.Direction.Y)
|
||||||
|
// Handle movement logic here
|
||||||
|
case "message":
|
||||||
|
fmt.Printf("Received message from Session ID: %s : %s\n", clients[key], payload.Message)
|
||||||
|
broadcastMessage(conn, clients, payload.Message)
|
||||||
|
default:
|
||||||
|
fmt.Printf("Received unknown message type Session ID: %s\n", clients[key])
|
||||||
}
|
}
|
||||||
broadcast <- msg
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleMessages() {
|
func broadcastMessage(conn *net.UDPConn, clients map[string]string, message string) {
|
||||||
for {
|
for clientKey := range clients {
|
||||||
msg := <-broadcast
|
// Parse the clientKey to get the *net.UDPAddr
|
||||||
for client := range clients {
|
clientAddr, err := net.ResolveUDPAddr("udp", clientKey)
|
||||||
if err := client.WriteMessage(websocket.TextMessage, msg); err != nil {
|
if err != nil {
|
||||||
client.Close()
|
fmt.Println("Error resolving UDP address:", err)
|
||||||
delete(clients, client)
|
continue
|
||||||
}
|
}
|
||||||
|
payload := Payload{
|
||||||
|
Type: "message",
|
||||||
|
Message: message,
|
||||||
}
|
}
|
||||||
|
response, _ := json.Marshal(payload)
|
||||||
|
conn.WriteToUDP(response, clientAddr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user