/
conn.go
239 lines (207 loc) · 4.98 KB
/
conn.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package websocket
import (
"context"
"fmt"
"net/http"
"time"
ws "github.com/gorilla/websocket"
"github.com/vmihailenco/msgpack/v4"
"github.com/yaegaki/hibari"
)
// ConnTransportOption configure ConnTransport
type ConnTransportOption struct {
EncoderDecoder hibari.AnyMessageEncoderDecoder
}
type connTransport struct {
ctx context.Context
cancel context.CancelFunc
ws *ws.Conn
encDec hibari.AnyMessageEncoderDecoder
}
const (
writeWait = 10 * time.Second
pongWait = 60 * time.Second
pingPeriod = (pongWait * 9) / 10
maxMessageSize = 5000
)
var upgrader = ws.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func (c *connTransport) Context() context.Context {
return context.Background()
}
func (c *connTransport) ReadMessage() (hibari.Message, error) {
msgType, bin, err := c.ws.ReadMessage()
if err != nil {
return hibari.Message{}, err
}
if msgType != ws.BinaryMessage {
return hibari.Message{}, fmt.Errorf("InvalidType message: %v", msgType)
}
var msg message
err = msgpack.Unmarshal(bin, &msg)
if err != nil {
return hibari.Message{}, fmt.Errorf("UnmarshalError")
}
var body interface{}
switch msg.Kind {
case hibari.JoinMessage:
temp, ok := msg.Body.(joinMessageBody)
if !ok {
break
}
body = hibari.JoinMessageBody{
UserID: temp.UserID,
Secret: temp.Secret,
RoomID: temp.RoomID,
}
case hibari.BroadcastMessage:
temp, ok := msg.Body.(broadcastMessageBody)
if !ok {
break
}
anyBody, err := c.encodeAnyMessageBody(temp)
if err != nil {
return hibari.Message{}, err
}
body = anyBody
case hibari.CustomMessage:
temp, ok := msg.Body.(customMessageBody)
if !ok {
break
}
anyBody, err := c.encodeAnyMessageBody(temp.Body)
if err != nil {
return hibari.Message{}, err
}
body = hibari.CustomMessageBody{
Kind: temp.Kind,
Body: anyBody,
}
}
if body == nil {
return hibari.Message{}, fmt.Errorf("Invalid body")
}
return hibari.Message{
Kind: msg.Kind,
Body: body,
}, nil
}
func (c *connTransport) WriteMessage(msg hibari.Message) error {
var body interface{}
switch msg.Kind {
case hibari.OnAuthenticationFailedMessage:
body = struct{}{}
case hibari.OnJoinFailedMessage:
body = struct{}{}
case hibari.OnJoinMessage:
temp, ok := msg.Body.(hibari.OnJoinMessageBody)
if !ok {
break
}
body = onJoinMessageBody{
UserMap: userMapFrom(temp.UserMap),
}
case hibari.OnOtherUserJoinMessage:
temp, ok := msg.Body.(hibari.OnOtherUserJoinMessageBody)
if !ok {
break
}
body = onOtherUserJoinMessageBody{
User: shortUserFrom(temp.User),
}
case hibari.OnOtherUserLeaveMessage:
temp, ok := msg.Body.(hibari.OnOtherUserLeaveMessageBody)
if !ok {
break
}
body = onOtherUserLeaveMessageBody{
User: shortUserFrom(temp.User),
}
case hibari.OnBroadcastMessage:
temp, ok := msg.Body.(hibari.OnBroadcastMessageBody)
if !ok {
break
}
bytes, err := c.decodeAnyMessageBody(temp.Body)
if err != nil {
return err
}
body = onBroadcastMessageBody{
From: shortUserFrom(temp.From),
Body: bytes,
}
default:
}
if body == nil {
return fmt.Errorf("Invalid body")
}
bin, err := msgpack.Marshal(message{
Kind: msg.Kind,
Body: body,
})
if err != nil {
return err
}
return c.ws.WriteMessage(ws.BinaryMessage, bin)
}
func (*connTransport) PingPeriod() time.Duration {
return pingPeriod
}
func (c *connTransport) Ping() error {
return c.ws.WriteMessage(ws.PingMessage, []byte{})
}
func (c *connTransport) Close() {
c.ws.WriteMessage(ws.CloseMessage, []byte{})
c.ws.Close()
c.cancel()
}
func (c *connTransport) encodeAnyMessageBody(body []byte) (interface{}, error) {
if c.encDec != nil {
result, err := c.encDec.EncodeAnyMessageBody(body)
if err != nil {
return nil, err
}
return result, nil
}
return body, nil
}
func (c *connTransport) decodeAnyMessageBody(body interface{}) ([]byte, error) {
if c.encDec != nil {
temp, err := c.encDec.DecodeAnyMessageBody(body)
if err != nil {
return nil, err
}
body = temp
}
bin, ok := body.([]byte)
if !ok {
return nil, fmt.Errorf("Invalid any message body")
}
return bin, nil
}
// ServeWs starts serve websocket
func ServeWs(m hibari.Manager, o ConnTransportOption, w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
ctx, cancel := context.WithCancel(context.Background())
c := &connTransport{
ctx: ctx,
cancel: cancel,
ws: ws,
encDec: o.EncoderDecoder,
}
c.ws.SetReadLimit(maxMessageSize)
c.ws.SetReadDeadline(time.Now().Add(pongWait))
c.ws.SetPongHandler(func(string) error {
c.ws.SetReadDeadline(time.Now().Add(pongWait))
return nil
})
hibari.StartConn(m, c, hibari.ConnOption{SendBufferSize: 10})
}