2023-04-27 09:03:40 +08:00
|
|
|
package reader
|
|
|
|
|
|
|
|
import (
|
2024-08-09 03:50:41 +08:00
|
|
|
"bbb-graphql-middleware/internal/common"
|
2024-06-25 21:27:44 +08:00
|
|
|
"bytes"
|
2023-04-27 09:03:40 +08:00
|
|
|
"context"
|
2024-06-25 21:27:44 +08:00
|
|
|
"encoding/json"
|
2024-03-13 21:35:51 +08:00
|
|
|
"errors"
|
2023-04-27 09:03:40 +08:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"nhooyr.io/websocket"
|
2023-04-27 09:26:55 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
2023-04-27 09:03:40 +08:00
|
|
|
)
|
|
|
|
|
2024-05-02 21:45:32 +08:00
|
|
|
func BrowserConnectionReader(
|
2024-07-06 00:35:08 +08:00
|
|
|
browserConnection *common.BrowserConnection,
|
2024-05-02 21:45:32 +08:00
|
|
|
waitGroups []*sync.WaitGroup) {
|
2024-07-06 00:35:08 +08:00
|
|
|
log := log.WithField("_routine", "BrowserConnectionReader").WithField("browserConnectionId", browserConnection.Id)
|
2023-09-30 07:05:23 +08:00
|
|
|
defer log.Debugf("finished")
|
|
|
|
log.Debugf("starting")
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
defer func() {
|
2024-07-06 00:35:08 +08:00
|
|
|
browserConnection.FromBrowserToHasuraChannel.Close()
|
|
|
|
browserConnection.FromBrowserToGqlActionsChannel.Close()
|
2023-04-27 09:26:55 +08:00
|
|
|
}()
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-07-06 00:35:08 +08:00
|
|
|
defer browserConnection.ContextCancelFunc()
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
for {
|
2024-07-06 00:35:08 +08:00
|
|
|
messageType, message, err := browserConnection.Websocket.Read(browserConnection.Context)
|
2023-04-27 09:03:40 +08:00
|
|
|
if err != nil {
|
2024-03-13 21:35:51 +08:00
|
|
|
if errors.Is(err, context.Canceled) {
|
|
|
|
log.Debugf("Closing Browser ws connection as Context was cancelled!")
|
|
|
|
} else {
|
2024-03-27 23:52:26 +08:00
|
|
|
log.Debugf("Browser is disconnected, skipping reading of ws message: %v", err)
|
2024-03-13 21:35:51 +08:00
|
|
|
}
|
2023-04-27 09:03:40 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 02:35:15 +08:00
|
|
|
log.Tracef("received from browser: %s", string(message))
|
2023-04-27 09:03:40 +08:00
|
|
|
|
2024-06-25 21:27:44 +08:00
|
|
|
if messageType != websocket.MessageText {
|
|
|
|
log.Warnf("received non-text message: %v", messageType)
|
2024-05-02 21:45:32 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-06-25 21:27:44 +08:00
|
|
|
var browserMessageType struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(message, &browserMessageType)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to unmarshal message: %v", err)
|
2024-06-29 03:53:11 +08:00
|
|
|
continue
|
2024-06-25 21:27:44 +08:00
|
|
|
}
|
2024-05-02 21:45:32 +08:00
|
|
|
|
2024-06-25 21:27:44 +08:00
|
|
|
if browserMessageType.Type == "subscribe" {
|
2024-07-05 04:00:06 +08:00
|
|
|
if bytes.Contains(message, []byte("\"query\":\"mutation")) {
|
2024-07-06 00:35:08 +08:00
|
|
|
browserConnection.FromBrowserToGqlActionsChannel.Send(message)
|
2024-06-25 21:27:44 +08:00
|
|
|
continue
|
2024-05-02 21:45:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-06 00:35:08 +08:00
|
|
|
browserConnection.FromBrowserToHasuraChannel.Send(message)
|
2023-04-27 09:03:40 +08:00
|
|
|
}
|
|
|
|
}
|