forked from hilerchyn/socketio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage.go
98 lines (76 loc) · 2.03 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
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
package socketio
import (
"errors"
"strconv"
"strings"
)
type Message struct {
Type int
ID string
Endpoint *Endpoint
Data string
}
// ParseMessages parses the given raw message in to Message.
func parseMessage(rawMsg string) (*Message, error) {
parts := strings.SplitN(rawMsg, ":", 4)
if len(parts) < 3 {
return nil, errors.New("Empty message")
}
msgType, err := strconv.Atoi(parts[0])
if err != nil {
return nil, err
}
id := parts[1]
endpoint := ParseEndpoint(parts[2])
data := ""
if len(parts) == 4 {
data = parts[3]
}
return &Message{msgType, id, endpoint, data}, nil
}
// String returns the string represenation of the Message.
func (m Message) String() string {
raw := strconv.Itoa(m.Type)
raw += ":" + m.ID
raw += ":"
if m.Endpoint != nil {
raw += m.Endpoint.String()
}
if m.Data != "" {
raw += ":" + m.Data
}
return raw
}
// NewDisconnect returns a new disconnect Message.
func NewDisconnect() *Message {
return &Message{Type: 0}
}
// NewConnect returns a new connect Message for the given endpoint.
func NewConnect(endpoint *Endpoint) *Message {
return &Message{Type: 1, Endpoint: endpoint}
}
// NewHeartbeat returns a new heartbeat Message.
func NewHeartbeat() *Message {
return &Message{Type: 2}
}
func NewMessageMsg(endpoint *Endpoint, msg string) *Message {
return &Message{Type: 3, Endpoint: endpoint, Data: msg}
}
func NewJSONMessage(endpoint *Endpoint, data string) *Message {
return &Message{Type: 4, Endpoint: endpoint, Data: data}
}
func NewEvent(endpoint *Endpoint, name string, args string) *Message {
return &Message{Type: 5, Endpoint: endpoint, Data: args}
}
func NewACK(data string) *Message {
return &Message{Type: 6, Data: data}
}
// NewError returns a new error Message for the given endpoint with a
// reason and an advice.
func NewError(endpoint *Endpoint, reason string, advice string) *Message {
return &Message{Type: 7, Endpoint: endpoint, Data: reason + "+" + advice}
}
// NewNoop returns a new no-op Message.
func NewNoop() *Message {
return &Message{Type: 8}
}