-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.go
142 lines (125 loc) · 3.21 KB
/
ws.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package gqlws
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/websocket"
)
const (
connectionInitMessage = "connection_init"
connectionACKMessage = "connection_ack"
connectionErrorMessage = "connection_error"
connectionKeepAliveMessage = "ka"
connectionTermination = "connection_terminate"
startMessage = "start"
dataMessage = "data"
errorMessage = "error"
completeMessage = "complete"
stopMessage = "stop"
graphQLWS = "graphql-ws"
)
type opPayload struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
OperationName string `json:"operationName"`
}
type message struct {
ID string `json:"id"`
Type string `json:"type"`
Payload json.RawMessage `json:"payload"`
}
type errorPayload struct {
Message string `json:"message"`
}
func shouldUpgrade(r *http.Request) bool {
for _, s := range websocket.Subprotocols(r) {
if s == graphQLWS {
return true
}
}
return false
}
func (h *handler) subscription(w http.ResponseWriter, r *http.Request) {
upgrader := websocket.Upgrader{
Subprotocols: []string{graphQLWS},
CheckOrigin: h.conf.CheckOrigin,
}
// Upgrading connection to WebSocket
socket, err := upgrader.Upgrade(w, r, nil)
if err != nil {
w.Write([]byte(fmt.Sprintf("Couldn't upgrade connection: %s", err)))
return
}
defer socket.Close()
closers := map[string]func(){}
ctx := r.Context()
for {
m := new(message)
err := socket.ReadJSON(m)
if err != nil {
return
}
switch m.Type {
case connectionInitMessage:
p := map[string]interface{}{}
err = json.Unmarshal(m.Payload, &p)
if err != nil {
send(socket, m.ID, errorMessage, errorPayload{err.Error()})
return
}
if ctx, err = h.conf.OnConnect(ctx, p); err != nil {
send(socket, m.ID, errorMessage, errorPayload{err.Error()})
return
}
send(socket, "", connectionACKMessage, nil)
case stopMessage:
closers[m.ID]()
err = send(socket, m.ID, completeMessage, nil)
if err != nil {
send(socket, m.ID, errorMessage, errorPayload{err.Error()})
return
}
case connectionTermination:
socket.Close()
return
case startMessage:
p := new(opPayload)
err = json.Unmarshal(m.Payload, &p)
if err != nil {
send(socket, m.ID, errorMessage, errorPayload{err.Error()})
return
}
ctx, cancel := context.WithCancel(ctx)
out, err := h.conf.Subscriber(ctx, p.Query, p.OperationName, p.Variables)
if err != nil {
send(socket, m.ID, errorMessage, errorPayload{err.Error()})
}
closers[m.ID] = cancel
go func() {
defer socket.Close()
for v := range out {
err := send(socket, m.ID, dataMessage, v)
if err != nil {
break
}
}
send(socket, m.ID, completeMessage, nil)
cancel()
delete(closers, m.ID)
}()
}
}
}
type sentMessage struct {
ID string `json:"id"`
Type string `json:"type"`
Payload interface{} `json:"payload"`
}
func send(socket *websocket.Conn, id string, mt string, payload interface{}) error {
return socket.WriteJSON(sentMessage{
ID: id,
Type: mt,
Payload: payload,
})
}