bigbluebutton-Github/bbb-graphql-middleware/internal/websrv/reader/reader.go
Gustavo Trott 095085310d
refactor: Graphql middleware enhancements (#19336)
* Convert all go chan to SafeChannel

* Remove unnecessary comments

* Refactor graphql message names

* Fix renamed function
2023-12-14 14:01:47 -03:00

49 lines
1.2 KiB
Go

package reader
import (
"context"
"github.com/iMDT/bbb-graphql-middleware/internal/common"
log "github.com/sirupsen/logrus"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
"sync"
"time"
)
func BrowserConnectionReader(browserConnectionId string, ctx context.Context, c *websocket.Conn, fromBrowserToHasuraChannel1 *common.SafeChannel, fromBrowserToHasuraChannel2 *common.SafeChannel, waitGroups []*sync.WaitGroup) {
log := log.WithField("_routine", "BrowserConnectionReader").WithField("browserConnectionId", browserConnectionId)
defer log.Debugf("finished")
log.Debugf("starting")
defer func() {
fromBrowserToHasuraChannel1.Close()
fromBrowserToHasuraChannel2.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)
}()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
for {
var v interface{}
err := wsjson.Read(ctx, c, &v)
if err != nil {
log.Debugf("Browser is disconnected, skiping reading of ws message: %v", err)
return
}
log.Tracef("received from browser: %v", v)
fromBrowserToHasuraChannel1.Send(v)
fromBrowserToHasuraChannel2.Send(v)
}
}