/
ws.go
50 lines (42 loc) · 1.42 KB
/
ws.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
// Copyright 2020-2021 Changkun Ou. All rights reserved.
// Use of this source code is governed by a GPL-3.0
// license that can be found in the LICENSE file.
package types
import (
"encoding/json"
"errors"
)
// Errors
var (
ErrBadAction = errors.New("bad action data")
)
// All actions from daemons
const (
ActionNone WebsocketAction = "none"
ActionHandshakeRegister = "register"
ActionHandshakeReady = "ready"
ActionClipboardChanged = "cbchanged"
ActionClipboardGet = "cbget"
ActionClipboardPut = "cbput"
ActionListDaemonsRequest = "lsdaemonreq"
ActionListDaemonsResponse = "lsdaemonsres"
ActionTerminate = "terminate"
)
// WebsocketAction is an action between midgard daemon and midgard server
type WebsocketAction string
// WebsocketMessage represents a message for websocket.
type WebsocketMessage struct {
Action WebsocketAction `json:"action"`
UserID string `json:"user_id"`
Message string `json:"msg"`
Data []byte `json:"data"` // action dependent data, json format
}
// Encode encodes a websocket message
func (m *WebsocketMessage) Encode() []byte {
b, _ := json.Marshal(m)
return b
}
// Decode decodes given data to m.
func (m *WebsocketMessage) Decode(data []byte) error {
return json.Unmarshal(data, m)
}