/
api_res.go
72 lines (56 loc) · 1.45 KB
/
api_res.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
package api_ws
import (
"github.com/deze333/vroom/errors"
"github.com/deze333/vroom/reqres"
"github.com/gorilla/websocket"
)
//------------------------------------------------------------
// Implementation
//------------------------------------------------------------
// Marshals successful data response.
func NewResponse(req *reqres.Req, data interface{}) []byte {
res := &reqres.Res{
Id: req.Id,
Op: req.Op,
Data: data,
}
return res.Marshal(req)
}
// Marshals broadcast response.
func NewResponse_Broadcast(id int, op string, data interface{}) []byte {
res := &reqres.Res{
Id: id,
Op: op,
Data: data,
}
return res.Marshal(nil)
}
// Marshals error condition.
func NewResponse_Err(req *reqres.Req, err *errors.ResError) []byte {
res := &reqres.Res{
Id: req.Id,
Op: req.Op,
Err: err,
}
return res.Marshal(req)
}
// Responds on websocket connection.
func Respond(conn *Conn, res []byte) (err error) {
wsConn := conn.conn
err = wsConn.WriteMessage(websocket.TextMessage, res)
if err == nil {
return
}
// TODO Include reporting in admin
/*
// Get session details
sess, _ := auth.GetSessionValues(conn.r)
// Report panic: err, url, params, session, stack
_onPanic(
fmt.Sprintf("WebSocket failed to write response, error: %v", err),
fmt.Sprintf("%v #%v @ %v", sess["initials"], sess["_auth"], sess["_ip"]),
"Response", string(res),
"Session", fmt.Sprint(sess))
*/
return
}