Skip to content

Commit

Permalink
fix: missing ws message (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
vuongxuongminh authored May 14, 2022
1 parent a03217e commit e5adb71
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
10 changes: 5 additions & 5 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ func (h *Handler) GraphQLHandle(w http.ResponseWriter, r *http.Request) {
return
}

h.addMetricsBeginRequest(gqlRequest)
defer func(startedAt time.Time) {
h.addMetricsEndRequest(gqlRequest, time.Since(startedAt))
}(time.Now())

if err = h.validateGraphqlRequest(gqlRequest); err != nil {
reporter.error = writeResponseErrors(err, w)

return
}

h.addMetricsBeginRequest(gqlRequest)
defer func(startedAt time.Time) {
h.addMetricsEndRequest(gqlRequest, time.Since(startedAt))
}(time.Now())

n := r.Context().Value(nextHandlerCtxKey).(caddyhttp.Handler)

if h.Caching != nil {
Expand Down
35 changes: 18 additions & 17 deletions ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,28 @@ func (c *wsConn) Read(b []byte) (n int, err error) {
buff.Reset()
buff.Write(b[:n])

r := wsutil.NewServerSideReader(buff)

if _, e := r.NextFrame(); e != nil {
return n, err
}
for {
msg := new(wsMessage)
request := new(graphql.Request)
data, _, e := wsutil.ReadClientData(buff)

decoder := json.NewDecoder(r)
msg := &wsMessage{}
if e != nil {
return n, err
}

if e := decoder.Decode(msg); e != nil {
return n, err
}
if e = json.Unmarshal(data, msg); e != nil {
continue
}

if msg.Type == "subscribe" || msg.Type == "start" {
request := new(graphql.Request)
if msg.Type != "subscribe" && msg.Type != "start" {
continue
}

if e := json.Unmarshal(msg.Payload, request); e != nil {
return n, err
if e = json.Unmarshal(msg.Payload, request); e != nil {
continue
}

if e := c.onWsSubscribe(request); e != nil {
if e = c.onWsSubscribe(request); e != nil {
c.writeErrorMessage(msg.ID, e)
c.writeCompleteMessage(msg.ID)

Expand All @@ -124,9 +125,9 @@ func (c *wsConn) Read(b []byte) (n int, err error) {

c.request = request
c.subscribeAt = time.Now()
}

return n, err
return n, err
}
}

func (c *wsConn) writeErrorMessage(id interface{}, errMsg error) error {
Expand Down

0 comments on commit e5adb71

Please sign in to comment.