pipeshandler

package module
v0.0.0-...-0b39949 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2025 License: Apache-2.0 Imports: 13 Imported by: 11

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateHandlerFor

func CreateHandlerFor[T any](instance *Instance, action string, handler func(*Context, T) pipes.Event)

Create a handler for an action using generics (with parsing already implemented)

func DefaultClientEncodingMiddleware

func DefaultClientEncodingMiddleware(client *Client, message []byte) ([]byte, error)

Default pipes-fiber encoding middleware (using JSON)

func DefaultDecodingMiddleware

func DefaultDecodingMiddleware(client *Client, bytes []byte) ([]byte, error)

Default pipes-fiber decoding middleware (using JSON)

func ErrorResponse

func ErrorResponse(ctx *Context, message localization.Translations, err error) pipes.Event

func NormalResponse

func NormalResponse(ctx *Context, data map[string]interface{}) pipes.Event

func Response

func Response(ctx *Context, data map[string]interface{}, instance *Instance) pipes.Event

func SuccessResponse

func SuccessResponse(ctx *Context) pipes.Event

func Translate

func Translate(c *Context, message localization.Translations) string

Translate any message on a request

Types

type Client

type Client struct {
	Conn    *websocket.Conn
	ID      string
	Session string
	End     time.Time
	Data    interface{}
	Mutex   *sync.Mutex
}

func (*Client) IsExpired

func (c *Client) IsExpired() bool

type Config

type Config struct {
	ExpectedConnections int64
	SessionDuration     time.Duration // How long a session should stay alive
	Secret              []byte        // JWT secret (for authorization)

	// Node handlers
	NodeDisconnectHandler func(node pipes.Node)

	// Client handlers
	ClientDisconnectHandler   func(client *Client)
	TokenValidateHandler      func(claims *ConnectionTokenClaims, attachments string) bool // Called before the websocket connection is accepted, returns if the client should be disconnected (true = disconnect)
	ClientConnectHandler      func(client *Client, attachments string) bool                // Called after websocket connection is established, returns if the client should be disconnected (true = disconnect)
	ClientEnterNetworkHandler func(client *Client, attachments string) bool                // Called after pipes adapter is registered, returns if the client should be disconnected (true = disconnect)

	// Codec middleware
	ClientEncodingMiddleware func(client *Client, instance *Instance, message []byte) ([]byte, error)
	DecodingMiddleware       func(client *Client, instance *Instance, message []byte) ([]byte, error)

	// Error handler
	ErrorHandler func(err error)
}

! If the functions aren't implemented pipesfiber will panic

type ConnectionTokenClaims

type ConnectionTokenClaims struct {
	Account        string `json:"acc"`             // Account id of the connecting client
	ExpiredUnixSec int64  `json:"e_u"`             // Expiration time in unix seconds
	Session        string `json:"ses"`             // Session id of the connecting client
	Node           string `json:"node"`            // Node id of the node the client is connecting to
	Extra          string `json:"extra,omitempty"` // Extra arguments for the connection

	jwt.RegisteredClaims
}

Connection token struct

func (ConnectionTokenClaims) ToClient

func (tk ConnectionTokenClaims) ToClient(conn *websocket.Conn, end time.Time) Client

type Context

type Context struct {
	Client     *Client
	Action     string // The action to perform
	Locale     string // The locale of the client
	ResponseId string
	Data       []byte
	Node       *pipes.LocalNode
	Instance   *Instance
}

type Instance

type Instance struct {
	Config Config
	// contains filtered or unexported fields
}

func Setup

func Setup(config Config) *Instance

func (*Instance) AddClient

func (instance *Instance) AddClient(client Client) *Client

func (*Instance) CheckToken

func (instance *Instance) CheckToken(token string, local *pipes.LocalNode) (*ConnectionTokenClaims, bool)

Check the JWT token

func (*Instance) Disconnect

func (instance *Instance) Disconnect(id string, session string)

Disconnect a client from the network

func (*Instance) ExistsConnection

func (instance *Instance) ExistsConnection(id string, session string) bool

func (*Instance) GenerateToken

func (instance *Instance) GenerateToken(account string, session string, extra string, node uint) (string, error)

Generate a connection token for a node

func (*Instance) Get

func (instance *Instance) Get(id string, session string) (*Client, bool)

func (*Instance) GetConnections

func (instance *Instance) GetConnections(id string) int

func (*Instance) GetSessions

func (instance *Instance) GetSessions(id string) []string

func (*Instance) Handle

func (instance *Instance) Handle(ctx *Context) bool

func (*Instance) Remove

func (instance *Instance) Remove(id string, session string)

Remove a session from the account (DOES NOT DISCONNECT, there is an extra method for that)

func (*Instance) ReportClientError

func (instance *Instance) ReportClientError(client *Client, context string, err error)

func (*Instance) ReportGeneralError

func (instance *Instance) ReportGeneralError(context string, err error)

func (*Instance) Send

func (instance *Instance) Send(id string, msg []byte)

func (*Instance) SendEventToOne

func (instance *Instance) SendEventToOne(c *Client, event pipes.Event) error

Sends an event to the only ONE session of the connected account

func (*Instance) SendMessage

func (instance *Instance) SendMessage(client *Client, msg []byte) error

func (*Instance) SendSession

func (instance *Instance) SendSession(id string, session string, msg []byte) bool

func (*Instance) SetupConnectionsCache

func (instance *Instance) SetupConnectionsCache(expected int64)

func (*Instance) UpdateClient

func (instance *Instance) UpdateClient(client *Client)

type Message

type Message[T any] struct {
	Action string `json:"action"`
	Data   T      `json:"data"`
}

Message received from the client

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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