bigbluebutton-Github/bbb-graphql-middleware/internal/websrv/reader/reader.go
Gustavo Trott cccd2d78c7
Refactor the logger to add more context like meetingId, userId and sessionToken. (#21188)
For DEBUG logs it also includes the file/line and function of the caller.
2024-09-18 08:48:36 -04:00

73 lines
1.8 KiB
Go

package reader
import (
"bbb-graphql-middleware/internal/common"
"bytes"
"context"
"encoding/json"
"errors"
"nhooyr.io/websocket"
"sync"
"time"
)
func BrowserConnectionReader(
browserConnection *common.BrowserConnection,
waitGroups []*sync.WaitGroup) {
defer browserConnection.Logger.Debugf("finished")
browserConnection.Logger.Debugf("starting")
defer func() {
browserConnection.FromBrowserToHasuraChannel.Close()
browserConnection.FromBrowserToGqlActionsChannel.Close()
}()
defer func() {
// Let other routines know this is about to die
for _, wg := range waitGroups {
wg.Done()
}
// Wait a little bit before closing the channels
time.Sleep(100 * time.Millisecond)
}()
defer browserConnection.ContextCancelFunc()
for {
messageType, message, err := browserConnection.Websocket.Read(browserConnection.Context)
if err != nil {
if errors.Is(err, context.Canceled) {
browserConnection.Logger.Debugf("Closing Browser ws connection as Context was cancelled!")
} else {
browserConnection.Logger.Debugf("Browser is disconnected, skipping reading of ws message: %v", err)
}
return
}
browserConnection.Logger.Tracef("received from browser: %s", string(message))
if messageType != websocket.MessageText {
browserConnection.Logger.Warnf("received non-text message: %v", messageType)
continue
}
var browserMessageType struct {
Type string `json:"type"`
}
err = json.Unmarshal(message, &browserMessageType)
if err != nil {
browserConnection.Logger.Errorf("failed to unmarshal message: %v", err)
continue
}
if browserMessageType.Type == "subscribe" {
if bytes.Contains(message, []byte("\"query\":\"mutation")) {
browserConnection.FromBrowserToGqlActionsChannel.Send(message)
continue
}
}
browserConnection.FromBrowserToHasuraChannel.Send(message)
}
}