client

package
v2.6.6 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 27 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	component.Base
	// contains filtered or unexported fields
}

Client is the client component.

It dials connections to the server and provides registration of routes, events and hooks as well as message sending and receiving.

func NewClient

func NewClient(opts ...Option) *Client

NewClient returns a new client component configured by the given options.

func (*Client) Close added in v2.6.0

func (c *Client) Close()

Close closes the client.

func (*Client) Destroy

func (c *Client) Destroy()

Destroy destroys the client.

It marks the client as shut, closes every connection and triggers the Destroy hook.

func (*Client) Init

func (c *Client) Init()

Init initializes the client.

It validates required configuration such as the network client and codec, terminating the process when they are missing, and then triggers the Init hook.

func (*Client) Name

func (c *Client) Name() string

Name returns the client name.

func (*Client) Proxy

func (c *Client) Proxy() *Proxy

Proxy returns the client proxy.

func (*Client) Start

func (c *Client) Start()

Start starts the component.

type Conn added in v2.1.0

type Conn struct {
	// contains filtered or unexported fields
}

Conn wraps a network connection.

It exposes message pushing, attribute management and basic information querying on top of the underlying network connection.

func (*Conn) Bind added in v2.1.0

func (c *Conn) Bind(uid int64)

Bind binds the given user ID to the connection.

func (*Conn) Close added in v2.1.0

func (c *Conn) Close() error

Close closes the connection.

func (*Conn) DelAttr added in v2.1.0

func (c *Conn) DelAttr(key any)

DelAttr deletes the attribute value of the given key.

func (*Conn) GetAttr added in v2.1.0

func (c *Conn) GetAttr(key any) value.Value

GetAttr returns the attribute value of the given key.

func (*Conn) ID added in v2.1.0

func (c *Conn) ID() int64

ID returns the connection ID.

func (*Conn) LocalAddr added in v2.1.0

func (c *Conn) LocalAddr() (net.Addr, error)

LocalAddr returns the local address, or an error when it cannot be determined.

func (*Conn) LocalIP added in v2.1.0

func (c *Conn) LocalIP() (string, error)

LocalIP returns the local IP address, or an error when it cannot be determined.

func (*Conn) Push added in v2.1.0

func (c *Conn) Push(message *cluster.Message) error

Push pushes message to the server after encoding and protecting its data.

When session security is enabled, the payload is sealed with the negotiated session key and the route and the sequence number are bound by the additional authenticated data.

func (*Conn) RemoteAddr added in v2.1.0

func (c *Conn) RemoteAddr() (net.Addr, error)

RemoteAddr returns the remote address, or an error when it cannot be determined.

func (*Conn) RemoteIP added in v2.1.0

func (c *Conn) RemoteIP() (string, error)

RemoteIP returns the remote IP address, or an error when it cannot be determined.

func (*Conn) SetAttr added in v2.1.0

func (c *Conn) SetAttr(key, value any)

SetAttr sets the attribute value of the given key.

func (*Conn) UID added in v2.1.0

func (c *Conn) UID() int64

UID returns the user ID.

func (*Conn) Unbind added in v2.1.0

func (c *Conn) Unbind()

Unbind unbinds the user ID from the connection.

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context is the route context.

It carries the connection and message information used by route handlers. A Context is valid only during the route handler call; it is recycled after the handler returns and must not be retained.

func (*Context) CID

func (c *Context) CID() int64

CID returns the connection ID.

func (*Context) Conn added in v2.1.0

func (c *Context) Conn() *Conn

Conn returns the connection.

func (*Context) Context

func (c *Context) Context() context.Context

Context returns the context.

func (*Context) Data

func (c *Context) Data() any

Data returns the message data.

When session security is enabled the payload has already been decrypted by the receive path.

func (*Context) Parse

func (c *Context) Parse(v any) (err error)

Parse unmarshals the message data into v. It returns an error when unmarshalling fails.

Session security decryption happens once in the receive path, so Parse is idempotent.

func (*Context) Route

func (c *Context) Route() int32

Route returns the message route.

func (*Context) Seq

func (c *Context) Seq() int32

Seq returns the message sequence number.

func (*Context) UID

func (c *Context) UID() int64

UID returns the user ID.

type DialOption added in v2.1.0

type DialOption func(o *dialOptions)

DialOption is a dial option.

func WithConnAttr added in v2.1.0

func WithConnAttr(key string, value any) DialOption

WithConnAttr sets a connection attribute.

func WithDialAddr added in v2.1.0

func WithDialAddr(addr string) DialOption

WithDialAddr sets the dial address.

type EventHandler

type EventHandler func(conn *Conn)

EventHandler is the event handler of the client.

type HookHandler added in v2.1.0

type HookHandler func(proxy *Proxy)

HookHandler is the hook handler of the client.

type Option

type Option func(o *options)

Option is a client option.

func WithClient

func WithClient(client network.Client) Option

WithClient sets the network client.

func WithCodec

func WithCodec(codec encoding.Codec) Option

WithCodec sets the codec.

func WithContext

func WithContext(ctx context.Context) Option

WithContext sets the context.

func WithEncryptor

func WithEncryptor(encryptor crypto.Encryptor) Option

WithEncryptor sets the message encryptor.

func WithID

func WithID(id string) Option

WithID sets the instance ID.

func WithName

func WithName(name string) Option

WithName sets the instance name.

func WithSessionSecurity added in v2.6.3

func WithSessionSecurity(signer crypto.Signer, suites ...aead.Suite) Option

WithSessionSecurity enables the session security between the client and the gate.

verifier verifies the signature of the gate, and it is usually built from the pinned gate public key. When no suite is given, the handshake defaults are used. When enabled, the payload is sealed with the negotiated session key, so the static encryptor is not applied.

type Proxy

type Proxy struct {
	// contains filtered or unexported fields
}

Proxy is the client proxy.

It exposes the registration of routes, events and hooks as well as dialing.

func (*Proxy) AddEventListener

func (p *Proxy) AddEventListener(event cluster.Event, handler EventHandler)

AddEventListener adds an event listener for the given event.

func (*Proxy) AddHookListener added in v2.1.0

func (p *Proxy) AddHookListener(hook cluster.Hook, handler HookHandler)

AddHookListener adds a hook listener for the given hook.

func (*Proxy) AddRouteHandler

func (p *Proxy) AddRouteHandler(route int32, handler RouteHandler)

AddRouteHandler adds a route handler for the given route.

func (*Proxy) Client added in v2.1.0

func (p *Proxy) Client() network.Client

Client returns the network client.

func (*Proxy) Dial added in v2.1.0

func (p *Proxy) Dial(opts ...DialOption) (*Conn, error)

Dial dials a connection to the server and returns the wrapped connection.

func (*Proxy) ID added in v2.1.0

func (p *Proxy) ID() string

ID returns the client ID.

func (*Proxy) Name added in v2.1.0

func (p *Proxy) Name() string

Name returns the client name.

func (*Proxy) SetDefaultRouteHandler

func (p *Proxy) SetDefaultRouteHandler(handler RouteHandler)

SetDefaultRouteHandler sets the default route handler that serves every unregistered route.

type RouteHandler

type RouteHandler func(ctx *Context)

RouteHandler is the route handler of the client.

The Context is valid only for the duration of the call and must not be retained after the handler returns.

Jump to

Keyboard shortcuts

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