-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayload.go
35 lines (29 loc) · 797 Bytes
/
payload.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
package engineio
import (
"bytes"
)
// Separator is the separator for packets.
//
// https://github.com/socketio/engine.io-protocol?tab=readme-ov-file#http-long-polling-1
const Separator = '\x1E'
// EncodePayload encodes packets into bytes.
func EncodePayload(packets []Packet) []byte {
encoded := make([][]byte, len(packets))
for i, packet := range packets {
encoded[i] = EncodePacket(packet)
}
return bytes.Join(encoded, []byte{Separator})
}
// DecodePayload decodes bytes into packets.
func DecodePayload(input []byte) ([]Packet, error) {
packets := bytes.Split(input, []byte{Separator})
decoded := make([]Packet, len(packets))
for i, packet := range packets {
p, err := DecodePacket(packet)
if err != nil {
return nil, err
}
decoded[i] = p
}
return decoded, nil
}