This is the golang parser for the socket.io protocol encoding, shared by both socket.io-client-go(not ready) and socket.io.
Compatibility table:
Parser version | Socket.IO server version | Protocol revision |
---|---|---|
1.x | 3.x | 5 |
socket.io-parser is the reference implementation of socket.io-protocol. Read the full API here: socket.io-protocol.
package main
import (
"github.com/zishang520/engine.io/v2/utils"
"github.com/zishang520/socket.io-go-parser/v2/parser"
)
func main() {
encoder := parser.NewEncoder()
id := uint64(13)
packet := &parser.Packet{
Type: parser.EVENT,
Data: []string{"test-packet"},
Id: &id,
}
encodedPackets := encoder.Encode(packet)
utils.Log().Default("encode %v", encodedPackets)
for _, encodedPacket := range encodedPackets {
decoder := parser.NewDecoder()
decoder.On("decoded", func(decodedPackets ...any) {
utils.Log().Default("decode %v", decodedPackets[0])
// decodedPackets[0].Type == parser.EVENT
// decodedPackets[0].Data == []string{"test-packet"}
// decodedPackets[0].Id == 13
})
decoder.Add(encodedPacket)
}
}
package main
import (
"github.com/zishang520/engine.io/v2/utils"
"github.com/zishang520/socket.io-go-parser/v2/parser"
)
func main() {
encoder := parser.NewEncoder()
id := uint64(13)
attachments := uint64(0)
packet := &parser.Packet{
Type: parser.BINARY_EVENT,
Data: []any{"test-packet", []byte{1, 2, 3, 4, 5}},
Id: &id,
Attachments: &attachments,
}
encodedPackets := encoder.Encode(packet)
utils.Log().Default("encode %v", encodedPackets)
for _, encodedPacket := range encodedPackets {
decoder := parser.NewDecoder()
decoder.On("decoded", func(decodedPackets ...any) {
utils.Log().Default("decode %v", decodedPackets[0])
// decodedPackets[0].Type == parser.BINARY_EVENT
// decodedPackets[0].Data == []any{"test-packet", []byte{1, 2, 3, 4, 5}}
// decodedPackets[0].Id == 13
})
decoder.Add(encodedPacket)
}
}
See the test suite for more examples of how socket.io-parser is used.
Standalone tests can be run with make test
which will run the golang tests.
You can run the tests locally using the following command:
make test
To contribute patches, run tests or benchmarks, make sure to clone the repository:
git clone git://github.com/zishang520/socket.io-go-parser.git
Then:
cd socket.io-go-parser
make test
See the Tests
section above for how to run tests before submitting any patches.
MIT