Documentation ¶
Overview ¶
Package protocol provides a object representation of a socket.io packet.
This package currently supports socket.io version 1 to socket.io version 5. It provides classes and API to read and write socket.io wire format, as represented by:
<packet type>[<# of binary attachments>-][<namespace>,][<acknowledgment id>][JSON-stringified payload without binary] [<binary attachment>]
or as a real example:
1-/admin,456["project:delete",{"_placeholder":true,"num":0}]
this is with the API:
Packet.Type Packet.AckID Packet.Namespace Packet.Data
This object is expected to be written and read directly to the connection. It's a streaming but transport agnostic.
Index ¶
- Constants
- type NewPacket
- type Option
- type Packet
- type PacketError
- type PacketV1
- func (pac *PacketV1) Copy(w io.Writer, r io.Reader) (n int64, err error)
- func (pac *PacketV1) GetAckID() uint64
- func (pac *PacketV1) GetData() interface{}
- func (pac *PacketV1) GetNamespace() string
- func (pac *PacketV1) GetType() byte
- func (pac PacketV1) Len() (n int)
- func (pac *PacketV1) Read(p []byte) (n int, err error)
- func (pac *PacketV1) ReadFrom(r io.Reader) (n int64, err error)
- func (pac *PacketV1) WithAckID(x uint64) Packet
- func (pac *PacketV1) WithData(x interface{}) Packet
- func (pac *PacketV1) WithNamespace(x string) Packet
- func (pac *PacketV1) WithOption(opts ...Option) Packet
- func (pac *PacketV1) WithType(x byte) Packet
- func (pac *PacketV1) Write(p []byte) (n int, err error)
- func (pac *PacketV1) WriteTo(w io.Writer) (n int64, err error)
- type PacketV2
- func (pac *PacketV2) Copy(w io.Writer, r io.Reader) (n int64, err error)
- func (pac *PacketV2) GetAckID() uint64
- func (pac *PacketV2) GetData() interface{}
- func (pac *PacketV2) GetNamespace() string
- func (pac *PacketV2) GetType() byte
- func (pac PacketV2) Len() (n int)
- func (pac *PacketV2) Read(p []byte) (n int, err error)
- func (pac *PacketV2) ReadFrom(r io.Reader) (n int64, err error)
- func (pac *PacketV2) WithAckID(x uint64) Packet
- func (pac *PacketV2) WithData(x interface{}) Packet
- func (pac *PacketV2) WithNamespace(x string) Packet
- func (pac *PacketV2) WithOption(opts ...Option) Packet
- func (pac *PacketV2) WithType(x byte) Packet
- func (pac *PacketV2) Write(p []byte) (n int, err error)
- func (pac *PacketV2) WriteTo(w io.Writer) (n int64, err error)
- type PacketV3
- func (pac *PacketV3) Copy(w io.Writer, r io.Reader) (n int64, err error)
- func (pac *PacketV3) GetAckID() uint64
- func (pac *PacketV3) GetData() interface{}
- func (pac *PacketV3) GetNamespace() string
- func (pac *PacketV3) GetType() byte
- func (pac PacketV3) Len() (n int)
- func (pac *PacketV3) Read(p []byte) (n int, err error)
- func (pac *PacketV3) ReadFrom(r io.Reader) (n int64, err error)
- func (pac *PacketV3) WithAckID(x uint64) Packet
- func (pac *PacketV3) WithData(x interface{}) Packet
- func (pac *PacketV3) WithNamespace(x string) Packet
- func (pac *PacketV3) WithOption(opts ...Option) Packet
- func (pac *PacketV3) WithType(x byte) Packet
- func (pac *PacketV3) Write(p []byte) (n int, err error)
- func (pac *PacketV3) WriteTo(w io.Writer) (n int64, err error)
- type PacketV4
- func (pac *PacketV4) Copy(w io.Writer, r io.Reader) (n int64, err error)
- func (pac *PacketV4) GetAckID() uint64
- func (pac *PacketV4) GetData() interface{}
- func (pac *PacketV4) GetNamespace() string
- func (pac *PacketV4) GetType() byte
- func (pac PacketV4) Len() (n int)
- func (pac *PacketV4) Read(p []byte) (n int, err error)
- func (pac *PacketV4) ReadBinary() (bin func(r io.Reader) error)
- func (pac *PacketV4) ReadFrom(r io.Reader) (n int64, err error)
- func (pac *PacketV4) WithAckID(x uint64) Packet
- func (pac *PacketV4) WithData(x interface{}) Packet
- func (pac *PacketV4) WithNamespace(x string) Packet
- func (pac *PacketV4) WithOption(opts ...Option) Packet
- func (pac *PacketV4) WithType(x byte) Packet
- func (pac *PacketV4) Write(p []byte) (n int, err error)
- func (pac *PacketV4) WriteTo(w io.Writer) (n int64, err error)
- type PacketV5
- func (pac *PacketV5) Copy(w io.Writer, r io.Reader) (n int64, err error)
- func (pac *PacketV5) GetAckID() uint64
- func (pac *PacketV5) GetData() interface{}
- func (pac *PacketV5) GetNamespace() string
- func (pac *PacketV5) GetType() byte
- func (pac PacketV5) Len() (n int)
- func (pac *PacketV5) Read(p []byte) (n int, err error)
- func (pac *PacketV5) ReadBinary() (bin func(r io.Reader) error)
- func (pac *PacketV5) ReadFrom(r io.Reader) (n int64, err error)
- func (pac *PacketV5) WithAckID(x uint64) Packet
- func (pac *PacketV5) WithData(x interface{}) Packet
- func (pac *PacketV5) WithNamespace(x string) Packet
- func (pac *PacketV5) WithOption(opts ...Option) Packet
- func (pac *PacketV5) WithType(x byte) Packet
- func (pac *PacketV5) Write(p []byte) (n int, err error)
- func (pac *PacketV5) WriteTo(w io.Writer) (n int64, err error)
Constants ¶
const ( ErrReadFailed erro.StringF = "failed to read bytes:: %w" ErrUnmarshalInitialFieldFailed erro.StringF = "failed to unmarshal initial fields:: %w" ErrMarshalDataFailed erro.StringF = "failed to marshal data:: %w" ErrUnmarshalDataFailed erro.StringF = "failed to unmarshal data:: %w" ErrMarshalBinaryDataFailed erro.StringF = "failed to marshal binary data:: %w" ErrUnmarshalBinaryDataFailed erro.StringF = "failed to unmarshal binary data:: %w" ErrParseIntFailed erro.StringF = "failed to parse int (%s):: %w" ErrUnexpectedPacketType erro.StringF = "unexpected data type %T" ErrUnexpectedAttachmentEnd erro.String = "unexpected attachment end" ErrUnexpectedJSONEnd erro.String = "unexpected JSON end" ErrBinaryDataUnsupported erro.String = "binary data unsupported in this version" ErrReadUseBuffer errsPacket = "%s: read buffer" ErrShortRead erro.State = "read: short" ErrShortWrite erro.State = "write: short" ErrEmptyDataArray erro.State = "data array: empty" )
All of the errors that can happen while reading or writing socket.io packets
const ( ErrDecodeBase64Failed erro.StringF = "failed to decode msgpack base64 field:: %w" ErrDecodeFieldFailed erro.StringF = "failed to decode msgpack field:: %w" ErrEncodeFieldFailed erro.StringF = "failed to encode msgpack field:: %w" )
const ( ConnectPacket packetType = iota DisconnectPacket EventPacket AckPacket ErrorPacket )
The packet type codes available in the socket.io protocol version 1
const (
BinaryAckPacket packetType = BinaryEventPacket + 1
)
const (
BinaryEventPacket packetType = ErrorPacket + 1
)
add a BINARY_EVENT packet type after the ERROR packet type
const (
ConnectErrorPacket packetType = ErrorPacket
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type NewPacket ¶
type NewPacket func() Packet
NewPacket provides an external type for a Packet factory function. This is mainly used when creating new socket.io protocol transports.
type Option ¶
type Option func(Packet)
Option is a type that is used for configuring an existing Packet types with data
func WithNamespace ¶
WithNamespace sets the Namespace field in a Packet object
type Packet ¶
type Packet interface { WithOption(...Option) Packet WithType(byte) Packet WithNamespace(string) Packet WithAckID(uint64) Packet WithData(interface{}) Packet }
Packet is the interface for objects that can be passed around as socket.io packets. It provides a fluent interface for adding data common data to the underling type. The WithOption method can be used to add data to underling types that have additional data than that standard, type, namespace, ackID and data.
func NewPacketV1 ¶
func NewPacketV1() Packet
NewPacketV1 returns a Packet interface that will read/write socket.io version 1 packets
func NewPacketV2 ¶
func NewPacketV2() Packet
NewPacketV2 returns a Packet interface that will read/write socket.io version 2 packets
func NewPacketV3 ¶
func NewPacketV3() Packet
func NewPacketV4 ¶
func NewPacketV4() Packet
func NewPacketV5 ¶
func NewPacketV5() Packet
type PacketError ¶
type PacketError struct {
// contains filtered or unexported fields
}
A PacketError holds buffered data that can be used in the event of an error. The underlining error is still sent, and can be compared with using errors.Is.
func (PacketError) Error ¶
func (e PacketError) Error() string
Error he method to allow this struct to be passed as an error
func (PacketError) Is ¶
func (e PacketError) Is(target error) bool
Is matches the target error to one of the underlining errors attached to this struct.
type PacketV1 ¶
type PacketV1 struct {
// contains filtered or unexported fields
}
PacketV1 embeds a base packet and will convert the SocketIO version 1 values
func (*PacketV1) Copy ¶
Copy forces an io.Copy to use the .Read and .Write methods to provide the copy
func (*PacketV1) GetAckID ¶
func (pac *PacketV1) GetAckID() uint64
GetAckID returns the underlining string type for a socket.io packet AckID
func (*PacketV1) GetData ¶
func (pac *PacketV1) GetData() interface{}
GetData returns the underlining data string/array type for a socket.io packet Data
func (*PacketV1) GetNamespace ¶
func (pac *PacketV1) GetNamespace() string
GetNamespace returns the underlining string type for a socket.io packet Namespace
func (*PacketV1) GetType ¶
func (pac *PacketV1) GetType() byte
GetType returns the underlining byte type for a socket.io packet Type
func (*PacketV1) Read ¶
Read writes out the PacketV1 object to a socket.io protocol version 1 wire format to p []bytes. This method can handle Read being called multiple times during the course of populating the []bytes.
func (*PacketV1) ReadFrom ¶
ReadFrom copies the []bytes from the socket.io wire format to the PacketV1 struct.
func (*PacketV1) WithAckID ¶
WithAckID sets the packet AckID to the x string (the underlining type for packetAckID) type. This is so that external packages don't need to know about protocol.PacketAckID types, but just the basic underlining type, we will convert it to the correct type.
func (*PacketV1) WithNamespace ¶
WithNamespace sets the packet Namespace to the x string (the underlining type for packetNS) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.
func (*PacketV1) WithOption ¶
WithOption applies all of the Option functions to a packet object, then returns the Packet interface. This method satisfies the interface for a Packet, so it allows packet(s) to be Packet(s).
func (*PacketV1) WithType ¶
WithType sets the packet Type to the x byte (the underlining type for packetType) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.
type PacketV2 ¶
type PacketV2 struct {
// contains filtered or unexported fields
}
PacketV2 embeds a base packet and will convert the SocketIO version 2 values
func (*PacketV2) Copy ¶
Copy forces an io.Copy to use the .Read and .Write methods to provide the copy
func (*PacketV2) GetAckID ¶
func (pac *PacketV2) GetAckID() uint64
GetAckID returns the underlining string type for a socket.io packet AckID
func (*PacketV2) GetData ¶
func (pac *PacketV2) GetData() interface{}
GetData returns the underlining data string/array type for a socket.io packet Data
func (*PacketV2) GetNamespace ¶
func (pac *PacketV2) GetNamespace() string
GetNamespace returns the underlining string type for a socket.io packet Namespace
func (*PacketV2) GetType ¶
func (pac *PacketV2) GetType() byte
GetType returns the underlining byte type for a socket.io packet Type
func (*PacketV2) Read ¶
Read writes out the PacketV2 object to a socket.io protocol version 2 wire format to p []bytes. This method can handle Read being called multiple times during the course of populating the []bytes.
func (*PacketV2) ReadFrom ¶
ReadFrom copies the []bytes from the socket.io wire format to the PacketV1 struct.
func (*PacketV2) WithAckID ¶
WithAckID sets the packet AckID to the x string (the underlining type for packetAckID) type. This is so that external packages don't need to know about protocol.PacketAckID types, but just the basic underlining type, we will convert it to the correct type.
func (*PacketV2) WithNamespace ¶
WithNamespace sets the packet Namespace to the x string (the underlining type for packetNS) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.
func (*PacketV2) WithOption ¶
WithOption applies all of the Option functions to a packet object, then returns the Packet interface. This method satisfies the interface for a Packet, so it allows packet(s) to be Packet(s).
func (*PacketV2) WithType ¶
WithType sets the packet Type to the x byte (the underlining type for packetType) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.
type PacketV3 ¶
type PacketV3 struct {
// contains filtered or unexported fields
}
func (*PacketV3) GetAckID ¶
func (pac *PacketV3) GetAckID() uint64
GetAckID returns the underlining string type for a socket.io packet AckID
func (*PacketV3) GetData ¶
func (pac *PacketV3) GetData() interface{}
GetData returns the underlining data string/array type for a socket.io packet Data
func (*PacketV3) GetNamespace ¶
func (pac *PacketV3) GetNamespace() string
GetNamespace returns the underlining string type for a socket.io packet Namespace
func (*PacketV3) GetType ¶
func (pac *PacketV3) GetType() byte
GetType returns the underlining byte type for a socket.io packet Type
func (*PacketV3) WithAckID ¶
WithAckID sets the packet AckID to the x string (the underlining type for packetAckID) type. This is so that external packages don't need to know about protocol.PacketAckID types, but just the basic underlining type, we will convert it to the correct type.
func (*PacketV3) WithData ¶
func (pac *PacketV3) WithData(x interface{}) Packet
WithData sets the packet Namespace to the x interface{} (the underlining type for packetData) type. This is so that external packages don't need to know about protocol.PacketDataString, protocol.PacketDataArray or protocol.PacketDataObject types, but just the basic underlining type, we will convert it to the correct type.
func (*PacketV3) WithNamespace ¶
WithNamespace sets the packet Namespace to the x string (the underlining type for packetNS) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.
func (*PacketV3) WithOption ¶
WithOption applies all of the Option functions to a packet object, then returns the Packet interface. This method satisfies the interface for a Packet, so it allows packet(s) to be Packet(s).
type PacketV4 ¶
type PacketV4 struct {
// contains filtered or unexported fields
}
func (*PacketV4) GetAckID ¶
func (pac *PacketV4) GetAckID() uint64
GetAckID returns the underlining string type for a socket.io packet AckID
func (*PacketV4) GetData ¶
func (pac *PacketV4) GetData() interface{}
GetData returns the underlining data string/array type for a socket.io packet Data
func (*PacketV4) GetNamespace ¶
func (pac *PacketV4) GetNamespace() string
GetNamespace returns the underlining string type for a socket.io packet Namespace
func (*PacketV4) GetType ¶
func (pac *PacketV4) GetType() byte
GetType returns the underlining byte type for a socket.io packet Type
func (*PacketV4) WithAckID ¶
WithAckID sets the packet AckID to the x string (the underlining type for packetAckID) type. This is so that external packages don't need to know about protocol.PacketAckID types, but just the basic underlining type, we will convert it to the correct type.
func (*PacketV4) WithData ¶
func (pac *PacketV4) WithData(x interface{}) Packet
WithData sets the packet Namespace to the x interface{} (the underlining type for packetData) type. This is so that external packages don't need to know about protocol.PacketDataString, protocol.PacketDataArray or protocol.PacketDataObject types, but just the basic underlining type, we will convert it to the correct type.
func (*PacketV4) WithNamespace ¶
WithNamespace sets the packet Namespace to the x string (the underlining type for packetNS) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.
func (*PacketV4) WithOption ¶
WithOption applies all of the Option functions to a packet object, then returns the Packet interface. This method satisfies the interface for a Packet, so it allows packet(s) to be Packet(s).
type PacketV5 ¶
type PacketV5 struct {
// contains filtered or unexported fields
}
func (*PacketV5) GetAckID ¶
func (pac *PacketV5) GetAckID() uint64
GetAckID returns the underlining string type for a socket.io packet AckID
func (*PacketV5) GetData ¶
func (pac *PacketV5) GetData() interface{}
GetData returns the underlining data string/array type for a socket.io packet Data
func (*PacketV5) GetNamespace ¶
func (pac *PacketV5) GetNamespace() string
GetNamespace returns the underlining string type for a socket.io packet Namespace
func (*PacketV5) GetType ¶
func (pac *PacketV5) GetType() byte
GetType returns the underlining byte type for a socket.io packet Type
func (*PacketV5) WithAckID ¶
WithAckID sets the packet AckID to the x string (the underlining type for packetAckID) type. This is so that external packages don't need to know about protocol.PacketAckID types, but just the basic underlining type, we will convert it to the correct type.
func (*PacketV5) WithData ¶
func (pac *PacketV5) WithData(x interface{}) Packet
WithData sets the packet Namespace to the x interface{} (the underlining type for packetData) type. This is so that external packages don't need to know about protocol.PacketDataString, protocol.PacketDataArray or protocol.PacketDataObject types, but just the basic underlining type, we will convert it to the correct type.
func (*PacketV5) WithNamespace ¶
WithNamespace sets the packet Namespace to the x string (the underlining type for packetNS) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.
func (*PacketV5) WithOption ¶
WithOption applies all of the Option functions to a packet object, then returns the Packet interface. This method satisfies the interface for a Packet, so it allows packet(s) to be Packet(s).