/
message.go
53 lines (43 loc) · 1.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
package util
//Message 收到数据的封装
type Message struct {
MsgID uint32 // 消息ID
DataLen uint32 // 消息长度
Data []byte // 消息
IsWebSocket bool // 是否为websocket协议
Opcode uint8 // 操作码
}
func (m *Message) ID() uint32 {
return m.MsgID
}
func (m *Message) String() string {
return string(m.Data)
}
//Bytes 获取Bytes
func (m *Message) Bytes() []byte {
return m.Data
}
//Len 获取长度
func (m *Message) Len() int {
return int(m.DataLen)
}
//SetData 设置数据
func (m *Message) SetData(bytes []byte) {
m.Data = bytes
m.DataLen = uint32(len(bytes)) // 更新长度
}
//GetOpcode 获取操作码,通过这个可以判断是否为websocket消息
func (m *Message) GetOpcode() uint8 {
return m.Opcode
}
func (m *Message) IsWebsocket() bool {
return m.IsWebSocket
}
//IsText 是否为websocket的文本消息
func (m *Message) IsText() bool {
return m.Opcode == 1
}
//IsBinary 是否为websocket的二进制消息
func (m *Message) IsBinary() bool {
return m.Opcode == 2
}