/
websocket.go
126 lines (117 loc) · 3.3 KB
/
websocket.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
// Code generated by goa v3.2.4, DO NOT EDIT.
//
// notifications WebSocket server streaming
//
// Command:
// $ goa gen github.com/fieldkit/cloud/server/api/design
package server
import (
"context"
"io"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
goahttp "goa.design/goa/v3/http"
)
// ConnConfigurer holds the websocket connection configurer functions for the
// streaming endpoints in "notifications" service.
type ConnConfigurer struct {
ListenFn goahttp.ConnConfigureFunc
}
// ListenServerStream implements the notifications.ListenServerStream interface.
type ListenServerStream struct {
once sync.Once
// upgrader is the websocket connection upgrader.
upgrader goahttp.Upgrader
// configurer is the websocket connection configurer.
configurer goahttp.ConnConfigureFunc
// cancel is the context cancellation function which cancels the request
// context when invoked.
cancel context.CancelFunc
// w is the HTTP response writer used in upgrading the connection.
w http.ResponseWriter
// r is the HTTP request.
r *http.Request
// conn is the underlying websocket connection.
conn *websocket.Conn
}
// NewConnConfigurer initializes the websocket connection configurer function
// with fn for all the streaming endpoints in "notifications" service.
func NewConnConfigurer(fn goahttp.ConnConfigureFunc) *ConnConfigurer {
return &ConnConfigurer{
ListenFn: fn,
}
}
// Send streams instances of "map[string]interface{}" to the "listen" endpoint
// websocket connection.
func (s *ListenServerStream) Send(v map[string]interface{}) error {
var err error
// Upgrade the HTTP connection to a websocket connection only once. Connection
// upgrade is done here so that authorization logic in the endpoint is executed
// before calling the actual service method which may call Send().
s.once.Do(func() {
var conn *websocket.Conn
conn, err = s.upgrader.Upgrade(s.w, s.r, nil)
if err != nil {
return
}
if s.configurer != nil {
conn = s.configurer(conn, s.cancel)
}
s.conn = conn
})
if err != nil {
return err
}
res := v
return s.conn.WriteJSON(res)
}
// Recv reads instances of "map[string]interface{}" from the "listen" endpoint
// websocket connection.
func (s *ListenServerStream) Recv() (map[string]interface{}, error) {
var (
rv map[string]interface{}
body map[string]interface{}
err error
)
// Upgrade the HTTP connection to a websocket connection only once. Connection
// upgrade is done here so that authorization logic in the endpoint is executed
// before calling the actual service method which may call Recv().
s.once.Do(func() {
var conn *websocket.Conn
conn, err = s.upgrader.Upgrade(s.w, s.r, nil)
if err != nil {
return
}
if s.configurer != nil {
conn = s.configurer(conn, s.cancel)
}
s.conn = conn
})
if err != nil {
return rv, err
}
if err = s.conn.ReadJSON(&body); err != nil {
return rv, err
}
if body == nil {
return rv, io.EOF
}
return body, nil
}
// Close closes the "listen" endpoint websocket connection.
func (s *ListenServerStream) Close() error {
var err error
if s.conn == nil {
return nil
}
if err = s.conn.WriteControl(
websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, "server closing connection"),
time.Now().Add(time.Second),
); err != nil {
return err
}
return s.conn.Close()
}