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

58 lines
1.6 KiB
Go
Raw Normal View History

2023-04-27 09:03:40 +08:00
package writer
import (
2024-08-09 03:50:41 +08:00
"bbb-graphql-middleware/internal/common"
"bytes"
"encoding/json"
2023-04-27 09:03:40 +08:00
log "github.com/sirupsen/logrus"
"nhooyr.io/websocket"
"sync"
2023-04-27 09:03:40 +08:00
)
func BrowserConnectionWriter(
browserConnection *common.BrowserConnection,
wg *sync.WaitGroup) {
log := log.WithField("_routine", "BrowserConnectionWriter").WithField("browserConnectionId", browserConnection.Id)
defer log.Debugf("finished")
log.Debugf("starting")
defer wg.Done()
2023-04-27 09:03:40 +08:00
RangeLoop:
for {
select {
case <-browserConnection.Context.Done():
log.Debug("Browser context cancelled.")
2023-04-27 09:03:40 +08:00
break RangeLoop
case toBrowserMessage := <-browserConnection.FromHasuraToBrowserChannel.ReceiveChannel():
2023-04-27 09:03:40 +08:00
{
if toBrowserMessage == nil {
if browserConnection.FromHasuraToBrowserChannel.Closed() {
2024-03-13 21:35:51 +08:00
break RangeLoop
}
continue
}
2024-07-01 08:26:02 +08:00
log.Tracef("sending to browser: %s", string(toBrowserMessage))
err := browserConnection.Websocket.Write(browserConnection.Context, websocket.MessageText, 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 bytes.Contains(toBrowserMessage, []byte("connection_error")) {
type HasuraMessage struct {
Type string `json:"type"`
}
var hasuraMessage HasuraMessage
_ = json.Unmarshal(toBrowserMessage, &hasuraMessage)
if hasuraMessage.Type == "connection_error" {
_ = browserConnection.Websocket.Close(websocket.StatusInternalError, string(toBrowserMessage))
}
2023-04-27 09:03:40 +08:00
}
}
}
}
}