-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
50 lines (42 loc) · 1.04 KB
/
message.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
package main
import (
"encoding/json"
"sort"
)
const (
messageKindCommand = "command"
messageOperationRead = "read"
messageOperationWrite = "write"
messageOperationDelete = "delete"
)
type Message struct {
Kind string `json:"kind"`
Operation string `json:"operation"`
Data interface{} `json:"data"`
}
// Process incoming message from web socket
func processIncomingMessage(m Message) {
dataStr := m.Data.(string)
if m.Kind == messageKindCommand {
switch m.Operation {
case messageOperationWrite:
var c Command
_ = json.Unmarshal([]byte(dataStr), &c)
updateUserCommand(c)
updateCommandLength(c.Command)
sort.Ints(sliceCommandLen)
writeCommandToDB(c.Title, c.Description, c.Command, c.Output)
case messageOperationDelete:
deleteCommandFromDB(dataStr)
}
}
}
// Create message struct to be written to web socket
func createMessage(kind string, operation string, jsonMsg interface{}) Message {
msg := Message{
Kind: kind,
Operation: operation,
Data: jsonMsg,
}
return msg
}