init commit
This commit is contained in:
5
go.mod
Normal file
5
go.mod
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
module mod-gateway-server
|
||||||
|
|
||||||
|
go 1.23.4
|
||||||
|
|
||||||
|
require github.com/gorilla/websocket v1.5.3 // indirect
|
||||||
2
go.sum
Normal file
2
go.sum
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
96
main.go
Normal file
96
main.go
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
var upgrader = websocket.Upgrader{
|
||||||
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
|
return true // Allow all origins for dev
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
conn *websocket.Conn
|
||||||
|
roomID string
|
||||||
|
userName string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Room struct {
|
||||||
|
clients map[*Client]bool
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
rooms = make(map[string]*Room)
|
||||||
|
roomsMu sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
|
func handleConnections(w http.ResponseWriter, r *http.Request) {
|
||||||
|
roomID := r.URL.Query().Get("roomId")
|
||||||
|
userName := r.URL.Query().Get("userName")
|
||||||
|
|
||||||
|
if roomID == "" || userName == "" {
|
||||||
|
http.Error(w, "Missing roomId or userName", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ws, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer ws.Close()
|
||||||
|
|
||||||
|
client := &Client{conn: ws, roomID: roomID, userName: userName}
|
||||||
|
|
||||||
|
roomsMu.Lock()
|
||||||
|
if rooms[roomID] == nil {
|
||||||
|
rooms[roomID] = &Room{clients: make(map[*Client]bool)}
|
||||||
|
}
|
||||||
|
room := rooms[roomID]
|
||||||
|
roomsMu.Unlock()
|
||||||
|
|
||||||
|
room.mu.Lock()
|
||||||
|
room.clients[client] = true
|
||||||
|
room.mu.Unlock()
|
||||||
|
|
||||||
|
log.Printf("User %s joined room %s", userName, roomID)
|
||||||
|
|
||||||
|
for {
|
||||||
|
_, msg, err := ws.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("User %s disconnected from room %s", userName, roomID)
|
||||||
|
room.mu.Lock()
|
||||||
|
delete(room.clients, client)
|
||||||
|
room.mu.Unlock()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Broadcast to everyone in the room except the sender
|
||||||
|
room.mu.Lock()
|
||||||
|
for c := range room.clients {
|
||||||
|
if c != client {
|
||||||
|
err := c.conn.WriteMessage(websocket.TextMessage, msg)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error: %v", err)
|
||||||
|
c.conn.Close()
|
||||||
|
delete(room.clients, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
room.mu.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/ws", handleConnections)
|
||||||
|
log.Println("Mod Gateway Server started on :8080")
|
||||||
|
err := http.ListenAndServe("192.168.0.164:56123", nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("ListenAndServe: ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user