Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

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

Gabe Kangasd4142df02163
diff --git a/core/chat/chatclient.go b/core/chat/chatclient.go
index 2cd1d67de4..a85511fb22 100644
--- a/core/chat/chatclient.go
+++ b/core/chat/chatclient.go
@@ -135,6 +135,14 @@ func (c *ChatClient) writePump() {
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 {
return
}
diff --git a/webroot/js/utils/websocket.js b/webroot/js/utils/websocket.js
index 19ede8902d..1b2433b78e 100644
--- a/webroot/js/utils/websocket.js
+++ b/webroot/js/utils/websocket.js
@@ -153,26 +153,31 @@ export default class Websocket {
pass it along to listeners.
*/
onMessage(e) {
- try {
- var model = JSON.parse(e.data);
- } catch (e) {
- // console.log(e, e.data);
- return;
+ // 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 {
+ var model = JSON.parse(e.data);
+ } catch (e) {
+ // console.log(e, e.data);
+ return;
+ }
+
+ if (!model.type) {
+ console.error('No type provided', model);
+ return;
+ }
+
+ // Send PONGs
+ if (model.type === SOCKET_MESSAGE_TYPES.PING) {
+ this.sendPong();
+ return;
+ }
+
+ // Notify any of the listeners via the raw socket message callback.
+ this.notifyRawMessageListeners(model);
}
-
- if (!model.type) {
- console.error('No type provided', model);
- return;
- }
-
- // Send PONGs
- if (model.type === SOCKET_MESSAGE_TYPES.PING) {
- this.sendPong();
- return;
- }
-
- // Notify any of the listeners via the raw socket message callback.
- this.notifyRawMessageListeners(model);
}
// Reply to a PING as a keep alive.