eioparser

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package eioparser implements the Engine.IO packet and payload codecs for both revisions of the protocol that are in use today:

  • revision 3, spoken by Engine.IO v3 clients (Socket.IO v2)
  • revision 4, spoken by Engine.IO v4+ clients (Socket.IO v3 and above)

It only depends on the Go standard library.

Index

Constants

View Source
const (
	ProtocolV3 = 3
	ProtocolV4 = 4
)

Protocol revisions understood by this package.

View Source
const (
	ContentTypeText   = "text/plain; charset=UTF-8"
	ContentTypeBinary = "application/octet-stream"
)

Content types used by the HTTP long-polling transport.

Variables

View Source
var ErrParser = errors.New("engine.io: parser error")

ErrParser is reported when a peer sends something that is not a valid Engine.IO packet or payload.

Functions

func EncodePacket

func EncodePacket(protocol int, p Packet, supportsBinary bool) (data []byte, isBinaryFrame bool)

EncodePacket serialises a single packet the way it is written to a WebSocket frame.

The returned isBinaryFrame reports whether the bytes must be sent as a binary WebSocket frame rather than a text one. When supportsBinary is false binary payloads are base64 encoded into a text frame instead.

Types

type Packet

type Packet struct {
	Type   Type
	Data   []byte
	Binary bool
}

Packet is a single Engine.IO packet.

Data holds the payload. When Binary is true the payload is an opaque byte string, otherwise it is UTF-8 text. Only Message packets may be binary.

func BinaryMessage

func BinaryMessage(data []byte) Packet

Binary returns a binary message packet.

func DecodePacket

func DecodePacket(protocol int, data []byte, isBinaryFrame bool) (Packet, error)

DecodePacket parses a single packet as received on a WebSocket frame.

func Text

func Text(t Type, data string) Packet

Text returns a text packet of the given type.

type Payload

type Payload struct {
	// Protocol is ProtocolV3 or ProtocolV4.
	Protocol int
	// SupportsBinary reports whether the peer can handle raw binary bodies.
	// It is always false for revision 4, which base64 encodes instead.
	SupportsBinary bool
}

Payload encodes and decodes the batches of packets exchanged over HTTP long-polling.

The two protocol revisions use completely different framings:

v3 text:   <length-in-utf16-code-units>:<packet>...
v3 binary: <0|1><length digits as bytes><0xff><packet>...
v4:        <packet>\x1e<packet>...

func (Payload) Decode

func (p Payload) Decode(body []byte, isBinaryBody bool) ([]Packet, error)

Decode parses an HTTP request body into packets. isBinaryBody must be true when the request was sent with the "application/octet-stream" content type.

func (Payload) Encode

func (p Payload) Encode(packets []Packet) (body []byte, contentType string)

Encode serialises packets into an HTTP response body and reports the Content-Type that must accompany it.

type Type

type Type byte

Type identifies an Engine.IO packet type. The numeric values are part of the wire format and are identical in both protocol revisions.

const (
	Open Type = iota
	Close
	Ping
	Pong
	Message
	Upgrade
	Noop
)

Engine.IO packet types.

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL