@@ -18,30 +18,30 @@ var upgrader = websocket.Upgrader{
18
18
// Client represents a WebSocket client
19
19
type Client struct {
20
20
conn * websocket.Conn
21
- secret string
21
+ roomID string
22
22
}
23
23
24
- // Hub maintains the set of active clients, broadcasts messages, and stores the current color per secret
24
+ // Hub maintains the set of active clients, broadcasts messages, and stores the current workspace per roomID
25
25
type Hub struct {
26
26
clients map [* Client ]bool
27
- colors map [string ]string // Store current color per secret
27
+ workspaces map [string ]string
28
28
broadcast chan Message
29
29
register chan * Client
30
30
unregister chan * Client
31
31
mutex sync.Mutex
32
32
}
33
33
34
- // Message represents a message with the associated secret
34
+ // Message represents a message with the associated roomID
35
35
type Message struct {
36
- secret string
36
+ roomID string
37
37
content []byte
38
38
}
39
39
40
40
// NewHub creates a new hub instance
41
41
func NewHub () * Hub {
42
42
return & Hub {
43
43
clients : make (map [* Client ]bool ),
44
- colors : make (map [string ]string ),
44
+ workspaces : make (map [string ]string ),
45
45
broadcast : make (chan Message ),
46
46
register : make (chan * Client ),
47
47
unregister : make (chan * Client ),
@@ -67,10 +67,10 @@ func (h *Hub) Run() {
67
67
68
68
case message := <- h .broadcast :
69
69
h .mutex .Lock ()
70
- // Update the current color for this secret
71
- h .colors [message .secret ] = string (message .content )
70
+ // Update the current workspace for this roomID
71
+ h .workspaces [message .roomID ] = string (message .content )
72
72
for client := range h .clients {
73
- if client .secret == message .secret {
73
+ if client .roomID == message .roomID {
74
74
err := client .conn .WriteMessage (websocket .TextMessage , message .content )
75
75
if err != nil {
76
76
log .Printf ("Error sending message: %v" , err )
@@ -86,9 +86,9 @@ func (h *Hub) Run() {
86
86
87
87
// ServeWs handles WebSocket requests
88
88
func serveWs (hub * Hub , c * gin.Context ) {
89
- secret := c .Query ("secret " )
90
- if secret == "" {
91
- http .Error (c .Writer , "Secret required" , http .StatusBadRequest )
89
+ roomID := c .Query ("roomID " )
90
+ if roomID == "" {
91
+ http .Error (c .Writer , "roomID required" , http .StatusBadRequest )
92
92
return
93
93
}
94
94
@@ -98,13 +98,12 @@ func serveWs(hub *Hub, c *gin.Context) {
98
98
return
99
99
}
100
100
101
- client := & Client {conn : conn , secret : secret }
101
+ client := & Client {conn : conn , roomID : roomID }
102
102
hub .register <- client
103
103
104
104
go handleMessages (client , hub )
105
105
}
106
106
107
- // HandleMessages listens for color messages from the client
108
107
func handleMessages (client * Client , hub * Hub ) {
109
108
defer func () {
110
109
hub .unregister <- client
@@ -118,31 +117,31 @@ func handleMessages(client *Client, hub *Hub) {
118
117
}
119
118
120
119
// Broadcast the message to other clients
121
- hub .broadcast <- Message {secret : client .secret , content : message }
120
+ hub .broadcast <- Message {roomID : client .roomID , content : message }
122
121
}
123
122
}
124
123
125
- // Status endpoint that shows the number of clients and the current color for each secret
124
+ // Status endpoint that shows the number of clients and the current color for each roomID
126
125
func statusHandler (hub * Hub ) gin.HandlerFunc {
127
126
return func (c * gin.Context ) {
128
127
hub .mutex .Lock ()
129
128
defer hub .mutex .Unlock ()
130
129
131
130
status := make (map [string ]interface {})
132
131
for client := range hub .clients {
133
- secret := client .secret
134
- currentStatus , ok := status [secret ]
132
+ roomID := client .roomID
133
+ currentStatus , ok := status [roomID ]
135
134
if ! ok {
136
- // Initialize status for a new secret
137
- status [secret ] = map [string ]interface {}{
135
+ // Initialize status for a new roomID
136
+ status [roomID ] = map [string ]interface {}{
138
137
"clients" : 1 ,
139
- "color " : hub .colors [ secret ],
138
+ "workspace " : hub .workspaces [ roomID ],
140
139
}
141
140
} else {
142
- // Update the client count for an existing secret
143
- status [secret ] = map [string ]interface {}{
141
+ // Update the client count for an existing roomID
142
+ status [roomID ] = map [string ]interface {}{
144
143
"clients" : currentStatus .(map [string ]interface {})["clients" ].(int ) + 1 ,
145
- "color " : hub .colors [ secret ],
144
+ "workspace " : hub .workspaces [ roomID ],
146
145
}
147
146
}
148
147
}
0 commit comments