-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
125 lines (105 loc) · 2.68 KB
/
protocol.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
package main
import (
"encoding/json"
"errors"
"github.com/gorilla/websocket"
)
type generalPacket struct {
Action string `json:"action"`
}
type clientRegister struct {
Action string `json:"action"`
ClientName string `json:"client-name"`
SecretKey string `json:"secret-key"`
}
func (clientRegister) GetID() int {
return 1
}
func (clientRegister) GetName() string {
return "register"
}
func (this *clientRegister) Decode(msg []byte) error {
err := json.Unmarshal(msg, this)
if err != nil {
return err
}
if (this.Action != this.GetName()) || (this.ClientName == "") || (this.SecretKey == "") {
return errors.New("invalid client register body")
}
return nil
}
type registerAck struct {
Action string `json:"action"`
}
func (registerAck) GetID() int {
return 2
}
func (registerAck) GetName() string {
return "register-ack"
}
func (this *registerAck) Construct() {
this.Action = this.GetName()
}
type clientMessage struct {
Action string `json:"action"`
Content string `json:"content"`
}
func (clientMessage) GetID() int {
return 3
}
func (clientMessage) GetName() string {
return "client-message"
}
func (this *clientMessage) Decode(msg []byte) error {
err := json.Unmarshal(msg, this)
if err != nil {
return err
}
if (this.Action != this.GetName()) || (this.Content == "") {
return errors.New("invalid client message body")
}
return nil
}
type forwardingMessage struct {
Action string `json:"action"`
SourceClientName string `json:"source-client-name"`
Content string `json:"content"`
}
func (forwardingMessage) GetID() int {
return 4
}
func (forwardingMessage) GetName() string {
return "forwarding-message"
}
func (this *forwardingMessage) Construct(clientName string, content string) {
this.Action = this.GetName()
this.SourceClientName = clientName
this.Content = content
}
func recvPacket(c *websocket.Conn) (int, []byte, error) {
mt, message, err := c.ReadMessage()
if err != nil {
return -1, nil, err
}
if mt != websocket.TextMessage {
return 0, nil, errors.New("wrong websocket message type")
}
var getAction generalPacket
err = json.Unmarshal(message, &getAction)
if err != nil {
return 0, message, errors.New("json decode error")
}
if (getAction.Action == clientRegister{}.GetName()) {
return clientRegister{}.GetID(), message, nil
}
if (getAction.Action == registerAck{}.GetName()) {
return registerAck{}.GetID(), message, nil
}
if (getAction.Action == clientMessage{}.GetName()) {
return clientMessage{}.GetID(), message, nil
}
if (getAction.Action == forwardingMessage{}.GetName()) {
return forwardingMessage{}.GetID(), message, nil
}
return 0, message, errors.New("unrecognized message type" + getAction.Action)
}