bigbluebutton-Github/bbb-graphql-middleware/internal/websrv/reader/reader.go

48 lines
1.1 KiB
Go
Raw Normal View History

2023-04-27 09:03:40 +08:00
package reader
import (
"context"
log "github.com/sirupsen/logrus"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
2023-04-27 09:26:55 +08:00
"sync"
"time"
2023-04-27 09:03:40 +08:00
)
func BrowserConnectionReader(browserConnectionId string, ctx context.Context, c *websocket.Conn, fromBrowserChannel1 chan interface{}, fromBrowserChannel2 chan interface{}, waitGroups []*sync.WaitGroup) {
log := log.WithField("_routine", "BrowserConnectionReader").WithField("browserConnectionId", browserConnectionId)
2023-04-27 09:03:40 +08:00
defer func() {
2023-04-27 09:26:55 +08:00
close(fromBrowserChannel1)
close(fromBrowserChannel2)
}()
defer func() {
// Let other routines know this is about to die
2023-04-27 09:03:40 +08:00
for _, wg := range waitGroups {
wg.Done()
}
2023-04-27 09:26:55 +08:00
// Wait a little bit before closing the channels
time.Sleep(100 * time.Millisecond)
2023-04-27 09:03:40 +08:00
}()
2023-04-27 09:26:55 +08:00
defer log.Debugf("finished")
2023-04-27 09:03:40 +08:00
for {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var v interface{}
err := wsjson.Read(ctx, c, &v)
if err != nil {
log.Errorf("error on read (browser is disconnected): %v", err)
return
}
log.Tracef("received from browser: %v", v)
fromBrowserChannel1 <- v
fromBrowserChannel2 <- v
}
}