-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_admin_socket.go
138 lines (120 loc) · 3.04 KB
/
web_admin_socket.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
package main
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/messaget/messaget/intent"
"gopkg.in/olahol/melody.v1"
)
var adminMelody = melody.New()
func handleControllerSocketEndpoint(c *gin.Context) {
err := adminMelody.HandleRequest(c.Writer, c.Request)
if err != nil {
err := c.AbortWithError(400, err)
if err != nil {
errorLogger.Println(err)
c.Abort()
return
}
}
}
type socketTransactionResponse struct {
Event string `json:"event"`
Failed bool `json:"failed"`
Id string `json:"transaction_id"`
Status int `json:"status"`
Response interface{} `json:"response"`
}
func setupAdminMelody() {
adminMelody.HandleError(func(session *melody.Session, err error) {
errorLogger.Println(err)
})
adminMelody.HandleConnect(func(s *melody.Session) {
// build Session
var remoteIp = readUserIp(s.Request)
id, session := NewSession("controller", remoteIp, s, true)
s.Set("Session", session)
infoLogger.Println("Accepted controller client", id)
})
adminMelody.HandleDisconnect(func(s *melody.Session) {
// get Session
var r, _ = s.Get("Session")
var session = r.(*Session)
infoLogger.Println("Dropping controller client", session.Id)
UnmapSession(session.Id, true)
})
adminMelody.HandleMessage(func(s *melody.Session, msg []byte) {
// parse
var i intent.Intent
if err := json.Unmarshal(msg, &i); err != nil {
errorLogger.Println(err)
// nothing? I suppose
return
}
// handle
handler, err := intentHandler.GetHandler(i.Intent)
if err != nil {
errorLogger.Println(err)
s.Write(serializeToBytes(socketTransactionResponse{
Event: "TRANSACTION",
Failed: true,
Id: i.TransactionId,
Status: 400,
Response: "Unknown intent " + i.Intent,
}))
return
}
response, statusCode, err := handler(i)
if err != nil {
// fail transaction ID
s.Write(serializeToBytes(socketTransactionResponse{
Event: "TRANSACTION",
Failed: true,
Id: i.TransactionId,
Status: statusCode,
Response: err,
}))
} else {
s.Write(serializeToBytes(socketTransactionResponse{
Event: "TRANSACTION",
Failed: false,
Id: i.TransactionId,
Status: statusCode,
Response: response,
}))
}
})
}
func serializeToBytes(i interface{}) []byte {
bolB, _ := json.Marshal(i)
return bolB
}
type UpdatePacket struct {
Event string `json:"event"`
Client Session `json:"client"`
}
func broadcastClientJoin(session *Session) {
adminSessionLock.RLock()
defer adminSessionLock.RUnlock()
for s := range adminSessionMap {
err := adminSessionMap[s].Ws.Write(serializeToBytes(UpdatePacket{
Event: "CLIENT_ADD",
Client: *session,
}))
if err != nil {
errorLogger.Println(err)
}
}
}
func broadcastClientLeave(session *Session) {
adminSessionLock.RLock()
defer adminSessionLock.RUnlock()
for s := range adminSessionMap {
err := adminSessionMap[s].Ws.Write(serializeToBytes(UpdatePacket{
Event: "CLIENT_LEAVE",
Client: *session,
}))
if err != nil {
errorLogger.Println(err)
}
}
}