@@ -6,10 +6,11 @@ package wshandler
66
77import (
88 "bytes"
9+ "encoding/json"
910 "fmt"
1011 "net/http"
12+ "os"
1113 "time"
12- "encoding/json"
1314
1415 "stack-web-app/frontend/db"
1516
@@ -155,8 +156,28 @@ func (c *Client) writePump() {
155156 c .conn .SetWriteDeadline (time .Now ().Add (writeWait ))
156157 if ! ok {
157158 // The hub closed the channel.
158- ContextLogger .Debug ("Hub has closed this channel." )
159+ ContextLogger .Debug ("Hub has closed this channel, sending update to users ." )
159160 c .conn .WriteMessage (websocket .CloseMessage , []byte {})
161+
162+ // Sending update to all still connected clients// Get current stack back and push to the broadcast message queue
163+ db .GetOffStack (c .hub .hubId , c .clientId )
164+ stackUsers , err := db .ShowCurrentStack (c .hub .hubId )
165+ if err != nil {
166+ ContextLogger .WithFields (log.Fields {
167+ "dbError" : err .Error (),
168+ }).Error ("Error getting current meeting stack contents." )
169+ }
170+ messageUsers , err := json .Marshal (stackUsers )
171+ if err != nil {
172+ ContextLogger .WithFields (log.Fields {
173+ "dbError" : err .Error (),
174+ }).Error ("Error marshalling JSON for response to client." )
175+ }
176+ message := bytes .TrimSpace (bytes .Replace (messageUsers , newline , space , - 1 ))
177+ ContextLogger .WithFields (log.Fields {
178+ "message" : fmt .Sprintf ("%+v" , string (message )),
179+ }).Debug ("Sending message from client to hub broadcast." )
180+ c .hub .broadcast <- message
160181 return
161182 }
162183
@@ -180,7 +201,8 @@ func (c *Client) writePump() {
180201 case <- ticker .C :
181202 c .conn .SetWriteDeadline (time .Now ().Add (writeWait ))
182203 if err := c .conn .WriteMessage (websocket .PingMessage , nil ); err != nil {
183- ContextLogger .Warning ("Error pinging the websocket I think." )
204+ ContextLogger .Warning ("Error pinging the websocket, assuming client is dead and unregistering." )
205+ c .hub .unregister <- c
184206 return
185207 }
186208 }
@@ -211,7 +233,7 @@ func GetWS(w http.ResponseWriter, r *http.Request) {
211233 }
212234
213235 // This is to enable local testing for myself. Probably stupid
214- _ , disableCORS := os .LookupEnv ("DISABLECORS " )
236+ _ , disableCORS := os .LookupEnv ("DISABLEWEBSOCKETORIGINCHECK " )
215237 if disableCORS {
216238 upgrader .CheckOrigin = func (r * http.Request ) bool { return true }
217239 }
0 commit comments