https://github.com/zishang520/socket.io-go-parser
socket.io-go-parser v4+
https://github.com/zishang520/socket.io-go-parser
Last synced: about 1 year ago
JSON representation
socket.io-go-parser v4+
- Host: GitHub
- URL: https://github.com/zishang520/socket.io-go-parser
- Owner: zishang520
- License: mit
- Created: 2023-07-24T03:47:49.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-08T03:51:39.000Z (about 1 year ago)
- Last Synced: 2025-04-15T00:43:12.324Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 64.5 KB
- Stars: 1
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# socket.io-go-parser
[](https://github.com/zishang520/socket.io-go-parser/actions)
[](https://pkg.go.dev/github.com/zishang520/socket.io-go-parser/v2)
This is the golang parser for the socket.io protocol encoding,
shared by both
[socket.io-client-go(not ready)](https://github.com/zishang520/socket.io-client-go) and
[socket.io](https://github.com/zishang520/socket.io).
Compatibility table:
| Parser version | Socket.IO server version | Protocol revision |
|----------------| ------------------------ | ----------------- |
| 1.x | 3.x | 5 |
## Parser API
socket.io-parser is the reference implementation of socket.io-protocol. Read
the full API here:
[socket.io-protocol](https://github.com/socketio/socket.io-protocol).
## Example Usage
### Encoding and decoding a packet
```go
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)
}
}
```
### Encoding and decoding a packet with binary data
```go
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.
## Tests
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
```
## Support
[issues](https://github.com/zishang520/socket.io-go-parser/issues)
## Development
To contribute patches, run tests or benchmarks, make sure to clone the
repository:
```bash
git clone git://github.com/zishang520/socket.io-go-parser.git
```
Then:
```bash
cd socket.io-go-parser
make test
```
See the `Tests` section above for how to run tests before submitting any patches.
## License
MIT