gocent

package module
v2.2.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2021 License: MIT Imports: 10 Imported by: 29

README

Gocent

Gocent is a Go HTTP API client for Centrifugo real-time messaging server.

Documentation

Installation

Install Gocent using the "go get" command:

go get github.com/centrifugal/gocent

License

Gocent is available under the MIT license.

Documentation

Overview

Package gocent is a Go language client for Centrifugo real-time messaging server HTTP API.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMalformedResponse can be returned when server replied with invalid response.
	ErrMalformedResponse = errors.New("malformed response returned from server")
	// ErrPipeEmpty returned when no commands found in Pipe.
	ErrPipeEmpty = errors.New("no commands in pipe")
)
View Source
var DefaultHTTPClient = &http.Client{Transport: &http.Transport{
	MaxIdleConnsPerHost: 100,
}, Timeout: time.Second}

DefaultHTTPClient will be used by default for HTTP requests.

Functions

This section is empty.

Types

type ChannelsResult

type ChannelsResult struct {
	Channels []string `json:"channels"`
}

ChannelsResult is a result of channels command.

type Client

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

Client is API client for project registered in server.

func New

func New(c Config) *Client

New returns initialized client instance based on provided config.

func (*Client) Broadcast

func (c *Client) Broadcast(ctx context.Context, channels []string, data []byte) error

Broadcast allows to broadcast the same data into many channels..

func (*Client) Channels

func (c *Client) Channels(ctx context.Context) (ChannelsResult, error)

Channels returns information about active channels (with one or more subscribers) on server.

func (*Client) Disconnect

func (c *Client) Disconnect(ctx context.Context, user string) error

Disconnect allows to close all connections of user to server.

func (*Client) History

func (c *Client) History(ctx context.Context, channel string) (HistoryResult, error)

History returns channel history.

func (*Client) HistoryRemove

func (c *Client) HistoryRemove(ctx context.Context, channel string) error

HistoryRemove removes channel history.

func (*Client) Info

func (c *Client) Info(ctx context.Context) (InfoResult, error)

Info returnes information about server nodes.

func (*Client) Pipe

func (c *Client) Pipe() *Pipe

Pipe allows to create new Pipe to send several commands in one HTTP request.

func (*Client) Presence

func (c *Client) Presence(ctx context.Context, channel string) (PresenceResult, error)

Presence returns channel presence information.

func (*Client) PresenceStats

func (c *Client) PresenceStats(ctx context.Context, channel string) (PresenceStatsResult, error)

PresenceStats returns short channel presence information (only counters).

func (*Client) Publish

func (c *Client) Publish(ctx context.Context, channel string, data []byte) error

Publish allows to publish data to channel.

func (*Client) SendPipe

func (c *Client) SendPipe(ctx context.Context, pipe *Pipe) ([]Reply, error)

SendPipe sends Commands collected in Pipe to Centrifugo. Using this method you should manually inspect all replies.

func (*Client) SetHTTPClient

func (c *Client) SetHTTPClient(httpClient *http.Client)

SetHTTPClient allows to set custom http Client to use for requests. Not goroutine-safe.

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(ctx context.Context, channel, user string) error

Unsubscribe allows to unsubscribe user from channel.

type ClientInfo

type ClientInfo struct {
	User     string          `json:"user"`
	Client   string          `json:"client"`
	ConnInfo json.RawMessage `json:"conn_info,omitempty"`
	ChanInfo json.RawMessage `json:"chan_info,omitempty"`
}

ClientInfo represents information about one client connection to Centrifugo. This struct used in messages published by clients, join/leave events, presence data.

type Command

type Command struct {
	Method string                 `json:"method"`
	Params map[string]interface{} `json:"params"`
}

Command represents API command to send.

type Config

type Config struct {
	// Addr is Centrifugo API endpoint.
	Addr string
	// GetAddr when set will be used before every API call to extract
	// Centrifugo API endpoint. In this case Addr field of Config will be
	// ignored. Nil value means using static Config.Addr field.
	GetAddr func() (string, error)
	// Key is Centrifugo API key.
	Key string
	// HTTPClient is a custom HTTP client to be used.
	// If nil DefaultHTTPClient will be used.
	HTTPClient *http.Client
}

Config of client.

type ErrStatusCode

type ErrStatusCode struct {
	Code int
}

ErrStatusCode can be returned in case request to server resulted in wrong status code.

func (ErrStatusCode) Error

func (e ErrStatusCode) Error() string

type Error

type Error struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

Error represents API request error.

func (Error) Error

func (e Error) Error() string

type HistoryResult

type HistoryResult struct {
	Publications []Publication `json:"publications"`
}

HistoryResult is a result of history command.

type InfoResult

type InfoResult struct {
	Nodes []NodeInfo `json:"nodes"`
}

InfoResult is a result of info command.

type NodeInfo

type NodeInfo struct {
	// UID is a unique id of running node.
	UID string `json:"uid"`
	// Name is a name of node (config defined or generated automatically).
	Name string `json:"name"`
	// Version of Centrifugo node.
	Version string `json:"version"`
	// NumClients is a number of clients connected to node.
	NumClients int `json:"num_clients"`
	// NumUsers is a number of unique users connected to node.
	NumUsers int `json:"num_users"`
	// NumChannels is a number of channels on node.
	NumChannels int `json:"num_channels"`
	// Uptime of node in seconds.
	Uptime int `json:"uptime"`
}

NodeInfo contains information and statistics about Centrifugo node.

type Pipe

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

Pipe allows to send several commands in one HTTP request.

func (*Pipe) AddBroadcast

func (p *Pipe) AddBroadcast(channels []string, data []byte) error

AddBroadcast adds broadcast command to client command buffer but not actually sends request to server until Pipe will be explicitly sent.

func (*Pipe) AddChannels

func (p *Pipe) AddChannels() error

AddChannels adds channels command to client command buffer but not actually sends request to server until Pipe will be explicitly sent.

func (*Pipe) AddDisconnect

func (p *Pipe) AddDisconnect(user string) error

AddDisconnect adds disconnect command to client command buffer but not actually sends request to server until Pipe will be explicitly sent.

func (*Pipe) AddHistory

func (p *Pipe) AddHistory(channel string) error

AddHistory adds history command to client command buffer but not actually sends request to server until Pipe will be explicitly sent.

func (*Pipe) AddHistoryRemove

func (p *Pipe) AddHistoryRemove(channel string) error

AddHistoryRemove adds history remove command to client command buffer but not actually sends request to server until Pipe will be explicitly sent.

func (*Pipe) AddInfo

func (p *Pipe) AddInfo() error

AddInfo adds info command to client command buffer but not actually sends request to server until Pipe will be explicitly sent.

func (*Pipe) AddPresence

func (p *Pipe) AddPresence(channel string) error

AddPresence adds presence command to client command buffer but not actually sends request to server until Pipe will be explicitly sent.

func (*Pipe) AddPresenceStats

func (p *Pipe) AddPresenceStats(channel string) error

AddPresenceStats adds presence stats command to client command buffer but not actually sends request to server until Pipe will be explicitly sent.

func (*Pipe) AddPublish

func (p *Pipe) AddPublish(channel string, data []byte) error

AddPublish adds publish command to client command buffer but not actually sends request to server until Pipe will be explicitly sent.

func (*Pipe) AddUnsubscribe

func (p *Pipe) AddUnsubscribe(channel string, user string) error

AddUnsubscribe adds unsubscribe command to client command buffer but not actually sends request to server until Pipe will be explicitly sent.

func (*Pipe) Reset

func (p *Pipe) Reset()

Reset allows to clear client command buffer.

type PresenceResult

type PresenceResult struct {
	Presence map[string]ClientInfo `json:"presence"`
}

PresenceResult is a result of presence command.

type PresenceStatsResult

type PresenceStatsResult struct {
	NumUsers   int32 `json:"num_users"`
	NumClients int32 `json:"num_clients"`
}

PresenceStatsResult is a result of info command.

type Publication

type Publication struct {
	UID     string          `json:"uid"`
	Info    *ClientInfo     `json:"info"`
	Channel string          `json:"channel"`
	Data    json.RawMessage `json:"data"`
	Client  string          `json:"client"`
}

Publication represents message published into channel.

type Reply

type Reply struct {
	Error  *Error          `json:"error"`
	Result json.RawMessage `json:"result"`
}

Reply is a server response to command.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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