client

package
v1.0.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2018 License: MIT Imports: 11 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client represents an instance of one of the servers clients

func NewClient

func NewClient(
	serverAddress string,
	implementation Implementation,
	opts Options,
) *Client

NewClient creates a new client instance.

func (*Client) Close

func (clt *Client) Close()

Close gracefully closes the connection and disables the client. A disabled client won't autoconnect until enabled again.

func (*Client) CloseSession

func (clt *Client) CloseSession() error

CloseSession disables the currently active session and acknowledges the server if connected. The session will be destroyed if this is it's last connection remaining. If the client is not connected then the synchronization is skipped. Does nothing if there's no active session

func (*Client) Connect

func (clt *Client) Connect() error

Connect connects the client to the configured server and returns an error in case of a connection failure. Automatically tries to restore the previous session. Enables autoconnect if it was disabled

func (*Client) PendingRequests

func (clt *Client) PendingRequests() int

PendingRequests returns the number of currently pending requests

func (*Client) Request

func (clt *Client) Request(
	name string,
	payload webwire.Payload,
) (webwire.Payload, error)

Request sends a request containing the given payload to the server and asynchronously returns the servers response blocking the calling goroutine. Returns an error if the request failed for some reason

func (*Client) RestoreSession

func (clt *Client) RestoreSession(sessionKey []byte) error

RestoreSession tries to restore the previously opened session. Fails if a session is currently already active

func (*Client) Session

func (clt *Client) Session() *webwire.Session

Session returns an exact copy of the session object or nil if there's no session currently assigned to this client

func (*Client) SessionInfo

func (clt *Client) SessionInfo(fieldName string) interface{}

SessionInfo returns a copy of the session info field value in the form of an empty interface to be casted to either concrete type

func (*Client) Signal

func (clt *Client) Signal(name string, payload webwire.Payload) error

Signal sends a signal containing the given payload to the server

func (*Client) Status

func (clt *Client) Status() Status

Status returns the current client status which is either disabled, disconnected or connected. The client is considered disabled when it was manually closed through client.Close, while disconnected is considered a temporary connection loss. A disabled client won't autoconnect until enabled again.

func (*Client) TimedRequest

func (clt *Client) TimedRequest(
	name string,
	payload webwire.Payload,
	timeout time.Duration,
) (webwire.Payload, error)

TimedRequest sends a request containing the given payload to the server and asynchronously returns the servers reply blocking the calling goroutine. Returns an error if the given timeout was exceeded awaiting the response or another failure occurred

type Implementation

type Implementation interface {
	// OnDisconnected is invoked when the client is disconnected
	// from the server for any reason.
	OnDisconnected()

	// OnSignal is invoked when the client receives a signal
	// from the server
	OnSignal(payload webwire.Payload)

	// OnSessionCreated is invoked when the client was assigned a new session
	OnSessionCreated(*webwire.Session)

	// OnSessionClosed is invoked when the client's session was closed
	// either by the server or the client itself
	OnSessionClosed()
}

Implementation defines a webwire client implementation interface

type Options

type Options struct {
	// SessionInfoParser defines the optional session info parser function
	SessionInfoParser webwire.SessionInfoParser

	// DefaultRequestTimeout defines the default request timeout duration
	// used by client.Request and client.RestoreSession
	DefaultRequestTimeout time.Duration

	// Autoconnect defines whether the autoconnect feature is to be enabled.
	//
	// If autoconnect is enabled then client.Request, client.TimedRequest and
	// client.RestoreSession won't immediately return a disconnected error
	// if there's no active connection to the server,
	// instead they will automatically try to reestablish the connection
	// before the timeout is triggered and a timeout error is returned.
	//
	// Autoconnect is enabled by default
	Autoconnect webwire.OptionValue

	// ReconnectionInterval defines the interval at which autoconnect
	// should retry connection establishment.
	// If undefined then the default value of 2 seconds is applied
	ReconnectionInterval time.Duration

	// WarnLog defines the warn logging output target
	WarnLog *log.Logger

	// ErrorLog defines the error logging output target
	ErrorLog *log.Logger
}

Options represents the options used during the creation a new client instance

func (*Options) SetDefaults

func (opts *Options) SetDefaults()

SetDefaults sets default values for undefined required options

type Status

type Status = int32

Status represents the status of a client instance

const (
	// StatDisconnected represents a temporarily disconnected client instance
	StatDisconnected Status = 0

	// StatConnected represents a connected client instance
	StatConnected Status = 1
)

Jump to

Keyboard shortcuts

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