gate

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2022 License: MIT Imports: 8 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClientNotExist = errors.New("client not exist")
View Source
var ErrInvalidID = errors.New("invalid id")

Functions

This section is empty.

Types

type Client

type Client interface {

	// SetID sets the ID of the client.
	SetID(id ID)

	// IsRunning returns true if the client is running/alive.
	IsRunning() bool

	// EnqueueMessage enqueues a message to be sent to the client.
	EnqueueMessage(message *messages.GlideMessage) error

	// Exit the client and close the connection.
	Exit()

	// Run starts the client message handling loop and blocks until the client.
	Run()

	// GetInfo returns the client's information.
	GetInfo() Info
}

Client is a client connection abstraction.

type Gateway

type Gateway interface {

	// SetClientID sets the client id with the new id.
	SetClientID(old ID, new_ ID) error

	// ExitClient exits the client with the given id.
	ExitClient(id ID) error

	// IsOnline returns true if the client is online.
	IsOnline(id ID) bool

	Interface
}

Gateway is the basic and common interface for all gate implementations. As the basic gate, it is used to provide a common gate interface for other modules to interact with the gate.

type ID

type ID string

ID is used to identify the client, the ID is consist of multiple parts, some of them are optional. The ID is constructed by concatenating the parts with a '_' separator, and the parts are:

  • The gateway id (optional): the string id of the gateway that the client is connected to.
  • The client id (required): the string id of the client, it is unique for user.
  • if the client is temporary, this id is a string generated by the gateway and start with `tmp`.
  • The client type (optional): the int type of the client, like 'web', 'mobile', 'desktop', etc.

func GenTempID added in v1.1.0

func GenTempID(gateID string) (ID, error)

GenTempID generates a temporary ID. ID implementation is a UUID v4, which is generated by random number generator.

func NewID

func NewID(gate string, uid string, device string) ID

NewID creates a new ID from the given user id, gateway id and client type.

func NewID2

func NewID2(uid string) ID

NewID2 creates a new ID from the given user id, use the empty gateway id and the empty client type.

func (*ID) Device

func (i *ID) Device() string

Device returns the device type of the client, if the client device type is not set, it returns "".

func (*ID) Gateway

func (i *ID) Gateway() string

Gateway returns the gateway id of the client, if not set, it returns an empty string.

func (*ID) IsTemp

func (i *ID) IsTemp() bool

IsTemp returns true if the ID is a temporary.

func (*ID) SetDevice added in v1.2.3

func (i *ID) SetDevice(device string) bool

SetDevice sets the device type of the client.

func (*ID) SetGateway added in v1.2.3

func (i *ID) SetGateway(gateway string) bool

SetGateway sets the gateway part of the ID.

func (*ID) UID

func (i *ID) UID() string

UID returns the user id of the client, if the client is temporary, it returns "".

type Info

type Info struct {

	// ID is the unique identifier for the client.
	ID ID

	// Version is the version of the client.
	Version string

	// AliveAt is the time the client was last seen.
	AliveAt int64

	// ConnectionAt is the time the client was connected.
	ConnectionAt int64

	// Gateway is the name of the gateway the client is connected to.
	Gateway string

	// CliAddr is the address of the client.
	CliAddr string
}

Info represents a client's information.

type Interface

type Interface interface {
	// EnqueueMessage enqueues the message to the client with the given id.
	EnqueueMessage(id ID, message *messages.GlideMessage) error
}

type MessageHandler

type MessageHandler func(cliInfo *Info, message *messages.GlideMessage)

MessageHandler used to handle messages from the gate.

type Server

type Server interface {
	Gateway

	// SetMessageHandler sets the client message handler.
	SetMessageHandler(h MessageHandler)

	// HandleConnection handles the new client connection and returns the random and temporary id set for the connection.
	HandleConnection(c conn.Connection) ID

	Run() error
}

Server is the interface for the gateway server, which is used to handle and manager client connections.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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