/
packets.go
44 lines (38 loc) · 859 Bytes
/
packets.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
package bilive
import (
"encoding/json"
"github.com/lainio/err2/try"
)
func NewJSONPacket(body []byte) (pkt Packet) {
pkt = Packet{
PacketLength: 16 + uint32(len(body)),
HeaderLength: 16,
ProtocolVersion: ProtocolNormalBuffer,
Operation: OpreationUserAuthentication,
SequenceID: 1,
Body: body,
}
return
}
func NewPingPacket() Packet {
return Packet{
PacketLength: 16,
HeaderLength: 16,
ProtocolVersion: ProtocolNormalBuffer,
Operation: OpreationHeartbeat,
SequenceID: 1,
}
}
func NewConnectPacket(roomid int) Packet {
er := EnterRoom{
UID: ref(0),
RoomID: roomid,
ProtoVer: ref(2),
Platform: ref("web"),
ClientVer: ref("2.0.11"),
Type: ref(2),
}
body := try.To1(json.Marshal(er))
return NewJSONPacket(body)
}
func ref[T any](t T) *T { return &t }