gowsps

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2022 License: MIT Imports: 9 Imported by: 0

README

GoWSPS

A go websocket packet system

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddHandler

func AddHandler[T any](s *PacketSystem, id VarInt, handler func(packet *T))

AddHandler adds a new packet handling function to the packet system for packets that have the provided id. The handler function will be called with the packet data whenever one is received

func MarshalPacket

func MarshalPacket(p *PacketBuffer, packet Packet) error

func UnMarshalPacket

func UnMarshalPacket(p *PacketBuffer, out any) error

Types

type Connection

type Connection struct {
	Lock        *sync.RWMutex
	Open        bool
	ReadBuffer  *PacketBuffer
	WriteBuffer *PacketBuffer

	*websocket.Conn
}

Connection structure for storing the current connection state as well as the reference to the websocket connection and write lock for preventing concurrently packet writes

func (*Connection) Send

func (conn *Connection) Send(packet Packet)

Send function for sending packets to the client. Will only send if the connection is open. Acquires write locks before sending packet

type ErrorHandler

type ErrorHandler func(err error)

ErrorHandler function that takes in errors

type Packet

type Packet struct {
	Id   VarInt
	Data any
}

type PacketBuffer

type PacketBuffer struct {
	*bytes.Buffer
}

func NewPacketBuffer

func NewPacketBuffer() *PacketBuffer

func (*PacketBuffer) ReadByteArray

func (p *PacketBuffer) ReadByteArray(length VarInt) ([]byte, error)

func (*PacketBuffer) ReadString

func (p *PacketBuffer) ReadString() (string, error)

func (*PacketBuffer) WriteString

func (p *PacketBuffer) WriteString(value string) error

func (*PacketBuffer) WriteVarInt

func (p *PacketBuffer) WriteVarInt(value VarInt) error

type PacketDecoder

type PacketDecoder func(c *Connection)

PacketDecoder structure of a function which decodes a packet

type PacketSystem

type PacketSystem struct {
	Handlers     map[VarInt]PacketDecoder
	ErrorHandler ErrorHandler
}

PacketSystem a structure for storing a list of handlers for different packet id values

func NewPacketSystem

func NewPacketSystem() *PacketSystem

NewPacketSystem creates a new packet system and returns a handle to the newly created packet system

func (*PacketSystem) DecodePacket

func (s *PacketSystem) DecodePacket(c *Connection) error

DecodePacket handles decoding of any packets received by the packet system. Uses the connection and the ReadJSON function to take the incoming data and then calls the handler function. This function will return an error if it failed to decode the packet

func (*PacketSystem) SetErrorHandler

func (s *PacketSystem) SetErrorHandler(handler ErrorHandler)

func (*PacketSystem) UpgradeAndListen

func (s *PacketSystem) UpgradeAndListen(w http.ResponseWriter, r *http.Request, callback func(conn *Connection, err error))

UpgradeAndListen upgrades the provided http connection to a websocket connection and listens for packet data in a loop. Calls the provided callback function before starting the loop

type VarInt

type VarInt uint64

Jump to

Keyboard shortcuts

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