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

52 lines
1.5 KiB
Go
Raw Normal View History

2023-04-27 09:03:40 +08:00
package writer
import (
"context"
"github.com/iMDT/bbb-graphql-middleware/internal/common"
2023-04-27 09:03:40 +08:00
log "github.com/sirupsen/logrus"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
"sync"
2023-04-27 09:03:40 +08:00
)
func BrowserConnectionWriter(browserConnectionId string, ctx context.Context, browserWsConn *websocket.Conn, fromHasuraToBrowserChannel *common.SafeChannel, wg *sync.WaitGroup) {
log := log.WithField("_routine", "BrowserConnectionWriter").WithField("browserConnectionId", browserConnectionId)
defer log.Debugf("finished")
log.Debugf("starting")
defer wg.Done()
2023-04-27 09:03:40 +08:00
RangeLoop:
for {
select {
case <-ctx.Done():
log.Debug("Browser context cancelled.")
2023-04-27 09:03:40 +08:00
break RangeLoop
case toBrowserMessage := <-fromHasuraToBrowserChannel.ReceiveChannel():
2023-04-27 09:03:40 +08:00
{
if toBrowserMessage == nil {
2024-03-13 21:35:51 +08:00
if fromHasuraToBrowserChannel.Closed() {
break RangeLoop
}
continue
}
var toBrowserMessageAsMap = toBrowserMessage.(map[string]interface{})
2023-04-27 09:03:40 +08:00
log.Tracef("sending to browser: %v", toBrowserMessage)
err := wsjson.Write(ctx, browserWsConn, toBrowserMessage)
2023-04-27 09:03:40 +08:00
if err != nil {
log.Debugf("Browser is disconnected, skipping writing of ws message: %v", err)
2023-04-27 09:03:40 +08:00
return
}
// After the error is sent to client, close its connection
// Authentication hook unauthorized this request
if toBrowserMessageAsMap["type"] == "connection_error" {
var payloadAsString = toBrowserMessageAsMap["payload"].(string)
browserWsConn.Close(websocket.StatusInternalError, payloadAsString)
2023-04-27 09:03:40 +08:00
}
}
}
}
}