From 89e2dbac1504d6df205938df750fa9eb15996afd Mon Sep 17 00:00:00 2001 From: Wim Date: Sat, 11 Mar 2023 18:14:49 +0100 Subject: Check client disconnect to exit for loop (api). Fixes #1983 (#2012) Also update to latest melody upstream --- bridge/api/api.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'bridge') diff --git a/bridge/api/api.go b/bridge/api/api.go index 515007f0..9e7679d3 100644 --- a/bridge/api/api.go +++ b/bridge/api/api.go @@ -6,7 +6,7 @@ import ( "sync" "time" - "gopkg.in/olahol/melody.v1" + "github.com/olahol/melody" "github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge/config" @@ -166,15 +166,20 @@ func (b *API) handleStream(c echo.Context) error { } c.Response().Flush() for { + select { // TODO: this causes issues, messages should be broadcasted to all connected clients - msg := b.Messages.Dequeue() - if msg != nil { - if err := json.NewEncoder(c.Response()).Encode(msg); err != nil { - return err + default: + msg := b.Messages.Dequeue() + if msg != nil { + if err := json.NewEncoder(c.Response()).Encode(msg); err != nil { + return err + } + c.Response().Flush() } - c.Response().Flush() + time.Sleep(100 * time.Millisecond) + case <-c.Request().Context().Done(): + return nil } - time.Sleep(200 * time.Millisecond) } } -- cgit v1.2.3