improved udp server
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
@@ -54,7 +55,8 @@ func main() {
|
|||||||
fmt.Println("Server running on " + port)
|
fmt.Println("Server running on " + port)
|
||||||
|
|
||||||
buf := make([]byte, 1024)
|
buf := make([]byte, 1024)
|
||||||
clients := make(map[string]string) // Map to keep track of clients and their session IDs
|
clients := make(map[string]string) // Map to keep track of clients and their session IDs
|
||||||
|
clientLastSeen := make(map[string]time.Time) // Map to track the last seen time of each client
|
||||||
|
|
||||||
for {
|
for {
|
||||||
n, addr, err := conn.ReadFromUDP(buf)
|
n, addr, err := conn.ReadFromUDP(buf)
|
||||||
@@ -73,34 +75,51 @@ func main() {
|
|||||||
// Use the client's IP and port as the key to uniquely identify the connection
|
// Use the client's IP and port as the key to uniquely identify the connection
|
||||||
key := addr.String()
|
key := addr.String()
|
||||||
|
|
||||||
|
// Update the last seen time for the client
|
||||||
|
clientLastSeen[key] = time.Now()
|
||||||
|
|
||||||
// Check if the client's connection already has a session ID
|
// Check if the client's connection already has a session ID
|
||||||
if _, ok := clients[key]; !ok {
|
if _, ok := clients[key]; !ok {
|
||||||
// The client is new, generate a session ID
|
// The client is new, generate a session ID
|
||||||
sessionID := generateSessionID()
|
sessionID := generateSessionID()
|
||||||
clients[key] = sessionID
|
clients[key] = sessionID
|
||||||
// Send the session ID back to the client
|
// Send the session ID back to the client
|
||||||
responsePayload := Payload{SessionID: sessionID}
|
responsePayload := Payload{Type: "init_success", SessionID: sessionID}
|
||||||
response, _ := json.Marshal(responsePayload)
|
response, _ := json.Marshal(responsePayload)
|
||||||
conn.WriteToUDP(response, addr)
|
conn.WriteToUDP(response, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch payload.Type {
|
switch payload.Type {
|
||||||
case "init":
|
case "init":
|
||||||
fmt.Printf("Received initiation generated SessionID: %s", clients[key])
|
fmt.Printf("Received initiation generated SessionID: %s\n", clients[key])
|
||||||
case "move":
|
case "move":
|
||||||
fmt.Printf("Received move message from Session ID: %s : X=%d, Y=%d\n", clients[key], payload.Direction.X, payload.Direction.Y)
|
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
|
// Handle movement logic here
|
||||||
|
case "disconnect":
|
||||||
|
// Disconnect the client
|
||||||
|
delete(clients, key)
|
||||||
|
delete(clientLastSeen, key)
|
||||||
|
fmt.Printf("Client %s disconnected\n", clients[key])
|
||||||
case "message":
|
case "message":
|
||||||
fmt.Printf("Received message from Session ID: %s : %s\n", clients[key], payload.Message)
|
fmt.Printf("Received message from Session ID: %s : %s\n", clients[key], payload.Message)
|
||||||
broadcastMessage(conn, clients, payload.Message)
|
broadcastMessage(conn, clients, payload.Message)
|
||||||
default:
|
default:
|
||||||
fmt.Printf("Received unknown message type Session ID: %s\n", clients[key])
|
fmt.Printf("Received unknown message type Session ID: %s\n", clients[key])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for disconnected clients and reset their session ID
|
||||||
|
for clientKey, lastSeen := range clientLastSeen {
|
||||||
|
if time.Since(lastSeen) > 5*time.Minute { // 5 minutes timeout
|
||||||
|
delete(clients, clientKey)
|
||||||
|
delete(clientLastSeen, clientKey)
|
||||||
|
fmt.Printf("Client %s disconnected and session ID reset\n", clients[clientKey])
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func broadcastMessage(conn *net.UDPConn, clients map[string]string, message string) {
|
func broadcastMessage(conn *net.UDPConn, clients map[string]string, message string) {
|
||||||
for clientKey := range clients {
|
for clientKey, sessionID := range clients {
|
||||||
// Parse the clientKey to get the *net.UDPAddr
|
// Parse the clientKey to get the *net.UDPAddr
|
||||||
clientAddr, err := net.ResolveUDPAddr("udp", clientKey)
|
clientAddr, err := net.ResolveUDPAddr("udp", clientKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -108,8 +127,9 @@ func broadcastMessage(conn *net.UDPConn, clients map[string]string, message stri
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
payload := Payload{
|
payload := Payload{
|
||||||
Type: "message",
|
Type: "message",
|
||||||
Message: message,
|
Message: message,
|
||||||
|
SessionID: sessionID,
|
||||||
}
|
}
|
||||||
response, _ := json.Marshal(payload)
|
response, _ := json.Marshal(payload)
|
||||||
conn.WriteToUDP(response, clientAddr)
|
conn.WriteToUDP(response, clientAddr)
|
||||||
|
Reference in New Issue
Block a user