Browse Source

Add in the optimization where multiple websocket events can exist within a single message

pull/1269/head
Gabe Kangas 4 years ago
parent
commit
fac06257ad
  1. 8
      core/chat/chatclient.go
  2. 5
      webroot/js/utils/websocket.js

8
core/chat/chatclient.go

@ -135,6 +135,14 @@ func (c *ChatClient) writePump() {
log.Debugln(err) log.Debugln(err)
} }
// Optimization: Send multiple events in a single websocket message.
// Add queued chat messages to the current websocket message.
n := len(c.send)
for i := 0; i < n; i++ {
_, _ = w.Write(newline)
_, _ = w.Write(<-c.send)
}
if err := w.Close(); err != nil { if err := w.Close(); err != nil {
return return
} }

5
webroot/js/utils/websocket.js

@ -153,6 +153,10 @@ export default class Websocket {
pass it along to listeners. pass it along to listeners.
*/ */
onMessage(e) { onMessage(e) {
// Optimization where multiple events can be sent within a
// single websocket message. So split them if needed.
var messages = e.data.split('\n');
for (var i = 0; i < messages.length; i++) {
try { try {
var model = JSON.parse(e.data); var model = JSON.parse(e.data);
} catch (e) { } catch (e) {
@ -174,6 +178,7 @@ export default class Websocket {
// Notify any of the listeners via the raw socket message callback. // Notify any of the listeners via the raw socket message callback.
this.notifyRawMessageListeners(model); this.notifyRawMessageListeners(model);
} }
}
// Reply to a PING as a keep alive. // Reply to a PING as a keep alive.
sendPong() { sendPong() {

Loading…
Cancel
Save