Skip to content

Commit ce18bfa

Browse files
authored
Merge pull request #7 from mpuckett159/bugs/update-on-disconnect
Fix issue where clients disconnecting doesn't visibly update stack for users
2 parents a6f1df9 + 487df4f commit ce18bfa

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

frontend/db/db.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func GetOnStack(tableId string, speakerId string, name string) (err error) {
144144
log.WithFields(log.Fields{
145145
"sqlQuery": addUserToStackTableSQL,
146146
"error": err.Error(),
147-
}).Error("Error preparing statement to delete meeting table")
147+
}).Error("Error preparing statement to get on stack.")
148148
return err
149149
}
150150
defer statement.Close()
@@ -155,7 +155,7 @@ func GetOnStack(tableId string, speakerId string, name string) (err error) {
155155
log.WithFields(log.Fields{
156156
"sqlQuery": addUserToStackTableSQL,
157157
"error": err.Error(),
158-
}).Error("Error executing statement to delete meeting table")
158+
}).Error("Error executing statement to get on stack.")
159159
return err
160160
}
161161

@@ -186,7 +186,7 @@ func GetOffStack(tableId string, speakerId string) (err error) {
186186
log.WithFields(log.Fields{
187187
"sqlQuery": removeUserFromStackTableSQL,
188188
"error": err.Error(),
189-
}).Error("Error preparing statement to delete meeting table")
189+
}).Error("Error preparing statement to get off stack")
190190
return err
191191
}
192192
defer statement.Close()
@@ -197,7 +197,7 @@ func GetOffStack(tableId string, speakerId string) (err error) {
197197
log.WithFields(log.Fields{
198198
"sqlQuery": removeUserFromStackTableSQL,
199199
"error": err.Error(),
200-
}).Error("Error executing statement to delete meeting table")
200+
}).Error("Error executing statement to get off stack")
201201
}
202202

203203
// Return nothing because there are no failures

frontend/wshandler/client.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ package wshandler
66

77
import (
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
}

frontend/wshandler/hub.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ func (h *Hub) run() {
9292
}).Debug("Client successfully registered to hub.")
9393
case client := <-h.unregister:
9494
if _, ok := h.clients[client]; ok {
95-
db.GetOffStack(h.hubId, client.clientId)
96-
delete(h.clients, client)
95+
// Closing the client connection
9796
close(client.send)
97+
_ = client.conn.Close()
98+
delete(h.clients, client)
9899
ContextLogger.WithFields(log.Fields{
99100
"client": fmt.Sprintf("%+v", client),
100101
"hub": fmt.Sprintf("%+v", h),

public/app.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ new Vue({
1111
},
1212

1313
created: function() {
14-
var self = this;
1514
let urlParams = new URLSearchParams(window.location.search);
1615
if (urlParams.has('meeting_id')) {
1716
this.tableId = urlParams.get('meeting_id');
1817
};
18+
if (window.location.protocol == "http:"){
19+
this.wsProtocol = "ws:"
20+
} else {
21+
this.wsProtocol = "wss:"
22+
};
1923
},
2024

2125
methods: {
@@ -63,7 +67,7 @@ new Vue({
6367

6468
// Join an on-going meeting by ID and update session values
6569
this.meetingUrl = window.location.host + '/?meeting_id=' + this.tableId;
66-
this.ws = new WebSocket('wss://' + window.location.host + '/ws?meeting_id=' + this.tableId);
70+
this.ws = new WebSocket(this.wsProtocol + '//' + window.location.host + '/ws?meeting_id=' + this.tableId);
6771
this.joined = true;
6872

6973
// Set up event listeners to handle incoming/outgoing messages and open/close actions
@@ -85,13 +89,13 @@ new Vue({
8589
method: "POST",
8690
headers: { "Accept": "application/json" }
8791
};
88-
await fetch("https://" + window.location.host + "/ws", requestOptions)
92+
await fetch(window.location.protocol + "//" + window.location.host + "/ws", requestOptions)
8993
.then(response => response.json())
9094
.then(data => self.tableId = data.meetingId);
9195

9296
// Set up new WebSocket to be used with the required meeting ID and update session values
9397
this.meetingUrl = window.location.host + '/?meeting_id=' + this.tableId;
94-
this.ws = new WebSocket('wss://' + window.location.host + '/ws?meeting_id=' + this.tableId);
98+
this.ws = new WebSocket(this.wsProtocol + '//' + window.location.host + '/ws?meeting_id=' + this.tableId);
9599
this.joined = true;
96100

97101
// Set up event listeners to handle incoming/outgoing messages and open/close actions

0 commit comments

Comments
 (0)