centrifuge

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2019 License: MIT Imports: 13 Imported by: 0

README

Websocket client for Centrifuge library and Centrifugo server.

Documentation

API documentation on Godoc

There is no v1 release of this library yet, API is not stable – so use with tools like dep or go mod.

License

MIT

Documentation

Index

Constants

View Source
const (
	DISCONNECTED = iota
	CONNECTING
	CONNECTED
	RECONNECTING
	CLOSED
)

Describe client connection statuses.

View Source
const (
	// DefaultHandshakeTimeout ...
	DefaultHandshakeTimeout = time.Second
	// DefaultReadTimeout ...
	DefaultReadTimeout = 5 * time.Second
	// DefaultWriteTimeout ...
	DefaultWriteTimeout = time.Second
	// DefaultPingInterval ...
	DefaultPingInterval = 25 * time.Second
	// DefaultPrivateChannelPrefix ...
	DefaultPrivateChannelPrefix = "$"
)
View Source
const (
	NEW = iota
	SUBSCRIBING
	SUBSCRIBED
	SUBERROR
	UNSUBSCRIBED
)

Describe different states of Sub.

Variables

View Source
var (
	// ErrTimeout ...
	ErrTimeout = errors.New("timeout")
	// ErrClientClosed ...
	ErrClientClosed = errors.New("client closed")
	// ErrClientDisconnected ...
	ErrClientDisconnected = errors.New("client disconnected")
	// ErrClientExpired ...
	ErrClientExpired = errors.New("client connection expired")
	// ErrReconnectFailed ...
	ErrReconnectFailed = errors.New("reconnect failed")
	// ErrDuplicateSubscription ...
	ErrDuplicateSubscription = errors.New("duplicate subscription")
)

Functions

This section is empty.

Types

type Client

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

Client describes client connection to Centrifugo server.

func New

func New(u string, config Config) *Client

New initializes Client.

func (*Client) Close

func (c *Client) Close() error

Close closes Client connection and cleans up state.

func (*Client) Connect

func (c *Client) Connect() error

Connect dials to server and sends connect message.

func (*Client) Disconnect

func (c *Client) Disconnect() error

Disconnect client from server.

func (*Client) NewSubscription added in v0.2.0

func (c *Client) NewSubscription(channel string) (*Subscription, error)

NewSubscription allows to create new subscription on channel.

func (*Client) OnConnect added in v0.2.0

func (c *Client) OnConnect(handler ConnectHandler)

OnConnect is a function to handle connect event.

func (*Client) OnDisconnect added in v0.2.0

func (c *Client) OnDisconnect(handler DisconnectHandler)

OnDisconnect is a function to handle disconnect event.

func (*Client) OnError added in v0.2.0

func (c *Client) OnError(handler ErrorHandler)

OnError is a function that will receive unhandled errors for logging.

func (*Client) OnMessage added in v0.2.0

func (c *Client) OnMessage(handler MessageHandler)

OnMessage allows to process async message from server to client.

func (*Client) OnPrivateSub added in v0.2.0

func (c *Client) OnPrivateSub(handler PrivateSubHandler)

OnPrivateSub needed to handle private channel subscriptions.

func (*Client) OnRefresh added in v0.2.0

func (c *Client) OnRefresh(handler RefreshHandler)

OnRefresh handles refresh event when client's credentials expired and must be refreshed.

func (*Client) Publish

func (c *Client) Publish(channel string, data []byte) error

Publish data into channel.

func (*Client) RPC

func (c *Client) RPC(data []byte) ([]byte, error)

RPC allows to make RPC – send data to server ant wait for response. RPC handler must be registered on server.

func (*Client) Send

func (c *Client) Send(data []byte) error

Send data to server asynchronously.

func (*Client) SetConnectData

func (c *Client) SetConnectData(data proto.Raw)

SetConnectData allows to set data to send in connect message.

func (*Client) SetHeader

func (c *Client) SetHeader(key, value string)

SetHeader allows to set custom header sent in Upgrade HTTP request.

func (*Client) SetToken

func (c *Client) SetToken(token string)

SetToken allows to set connection JWT token to let client authenticate itself on connect.

type ClientInfo

type ClientInfo = proto.ClientInfo

ClientInfo is short information about client connection.

type Config

type Config struct {
	// PrivateChannelPrefix is private channel prefix.
	PrivateChannelPrefix string
	// ReadTimeout is how long to wait read operations to complete.
	ReadTimeout time.Duration
	// WriteTimeout is Websocket write timeout.
	WriteTimeout time.Duration
	// PingInterval is how often to send ping commands to server.
	PingInterval time.Duration
	// HandshakeTimeout specifies the duration for the handshake to complete.
	HandshakeTimeout time.Duration
	// TLSConfig specifies the TLS configuration to use with tls.Client.
	// If nil, the default configuration is used.
	TLSConfig *tls.Config
	// EnableCompression specifies if the client should attempt to negotiate
	// per message compression (RFC 7692). Setting this value to true does not
	// guarantee that compression will be supported. Currently only "no context
	// takeover" modes are supported.
	EnableCompression bool
	// CookieJar specifies the cookie jar.
	// If CookieJar is nil, cookies are not sent in requests and ignored
	// in responses.
	CookieJar http.CookieJar
	// Header specifies custom HTTP Header to send.
	Header http.Header
}

Config contains various client options.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns Config with default options.

type ConnectEvent

type ConnectEvent struct {
	ClientID string
	Version  string
	Data     []byte
}

ConnectEvent is a connect event context passed to OnConnect callback.

type ConnectHandler

type ConnectHandler interface {
	OnConnect(*Client, ConnectEvent)
}

ConnectHandler is an interface describing how to handle connect event.

type DisconnectEvent

type DisconnectEvent struct {
	Reason    string
	Reconnect bool
}

DisconnectEvent is a disconnect event context passed to OnDisconnect callback.

type DisconnectHandler

type DisconnectHandler interface {
	OnDisconnect(*Client, DisconnectEvent)
}

DisconnectHandler is an interface describing how to handle disconnect event.

type Error

type Error = proto.Error

Error represents client reply error.

type ErrorEvent

type ErrorEvent struct {
	Message string
}

ErrorEvent is an error event context passed to OnError callback.

type ErrorHandler

type ErrorHandler interface {
	OnError(*Client, ErrorEvent)
}

ErrorHandler is an interface describing how to handle error event.

type EventHub

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

EventHub has all event handlers for client.

type JoinEvent

type JoinEvent struct {
	ClientInfo
}

JoinEvent has info about user who joined channel.

type JoinHandler

type JoinHandler interface {
	OnJoin(*Subscription, JoinEvent)
}

JoinHandler is a function to handle join messages.

type LeaveEvent

type LeaveEvent struct {
	ClientInfo
}

LeaveEvent has info about user who left channel.

type LeaveHandler

type LeaveHandler interface {
	OnLeave(*Subscription, LeaveEvent)
}

LeaveHandler is a function to handle leave messages.

type MessageEvent

type MessageEvent struct {
	Data []byte
}

MessageEvent is an event for async message from server to client.

type MessageHandler

type MessageHandler interface {
	OnMessage(*Client, MessageEvent)
}

MessageHandler is an interface describing how to async message from server.

type PresenceStats

type PresenceStats struct {
	NumClients int
	NumUsers   int
}

PresenceStats represents short presence information.

type PrivateSubEvent

type PrivateSubEvent struct {
	ClientID string
	Channel  string
}

PrivateSubEvent contains info required to create PrivateSign when client wants to subscribe on private channel.

type PrivateSubHandler

type PrivateSubHandler interface {
	OnPrivateSub(*Client, PrivateSubEvent) (string, error)
}

PrivateSubHandler is an interface describing how to handle private subscription request.

type Publication

type Publication = proto.Publication

Publication allows to deliver custom payload to all channel subscribers.

type PublishEvent

type PublishEvent struct {
	Publication
}

PublishEvent has info about received channel Publication.

type PublishHandler

type PublishHandler interface {
	OnPublish(*Subscription, PublishEvent)
}

PublishHandler is a function to handle messages published in channels.

type RefreshHandler

type RefreshHandler interface {
	OnRefresh(*Client) (string, error)
}

RefreshHandler is an interface describing how to handle token refresh event.

type SubscribeErrorEvent

type SubscribeErrorEvent struct {
	Error string
}

SubscribeErrorEvent is a subscribe error event context passed to event callback.

type SubscribeErrorHandler

type SubscribeErrorHandler interface {
	OnSubscribeError(*Subscription, SubscribeErrorEvent)
}

SubscribeErrorHandler is a function to handle subscribe error event.

type SubscribeSuccessEvent

type SubscribeSuccessEvent struct {
	Resubscribed bool
	Recovered    bool
}

SubscribeSuccessEvent is a subscribe success event context passed to event callback.

type SubscribeSuccessHandler

type SubscribeSuccessHandler interface {
	OnSubscribeSuccess(*Subscription, SubscribeSuccessEvent)
}

SubscribeSuccessHandler is a function to handle subscribe success event.

type Subscription

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

Subscription represents client subscription to channel.

func (*Subscription) Channel

func (s *Subscription) Channel() string

Channel returns subscription channel.

func (*Subscription) History

func (s *Subscription) History() ([]Publication, error)

History allows to extract channel history.

func (*Subscription) OnJoin added in v0.2.0

func (s *Subscription) OnJoin(handler JoinHandler)

OnJoin allows to set JoinHandler to SubEventHandler.

func (*Subscription) OnLeave added in v0.2.0

func (s *Subscription) OnLeave(handler LeaveHandler)

OnLeave allows to set LeaveHandler to SubEventHandler.

func (*Subscription) OnPublish added in v0.2.0

func (s *Subscription) OnPublish(handler PublishHandler)

OnPublish allows to set PublishHandler to SubEventHandler.

func (*Subscription) OnSubscribeError added in v0.2.0

func (s *Subscription) OnSubscribeError(handler SubscribeErrorHandler)

OnSubscribeError allows to set SubscribeErrorHandler to SubEventHandler.

func (*Subscription) OnSubscribeSuccess added in v0.2.0

func (s *Subscription) OnSubscribeSuccess(handler SubscribeSuccessHandler)

OnSubscribeSuccess allows to set SubscribeSuccessHandler to SubEventHandler.

func (*Subscription) OnUnsubscribe added in v0.2.0

func (s *Subscription) OnUnsubscribe(handler UnsubscribeHandler)

OnUnsubscribe allows to set UnsubscribeHandler to SubEventHandler.

func (*Subscription) Presence

func (s *Subscription) Presence() (map[string]ClientInfo, error)

Presence allows to extract channel history.

func (*Subscription) PresenceStats

func (s *Subscription) PresenceStats() (PresenceStats, error)

PresenceStats allows to extract channel presence stats.

func (*Subscription) Publish

func (s *Subscription) Publish(data []byte) error

Publish allows to publish data to channel.

func (*Subscription) Status

func (s *Subscription) Status() int

Status returns current Subscription status.

func (*Subscription) Subscribe

func (s *Subscription) Subscribe() error

Subscribe allows to subscribe again after unsubscribing.

func (*Subscription) Unsubscribe

func (s *Subscription) Unsubscribe() error

Unsubscribe allows to unsubscribe from channel.

type SubscriptionEventHub

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

SubscriptionEventHub contains callback functions that will be called when corresponding event happens with subscription to channel.

type UnsubscribeEvent

type UnsubscribeEvent struct{}

UnsubscribeEvent is an event passed to unsubscribe event handler.

type UnsubscribeHandler

type UnsubscribeHandler interface {
	OnUnsubscribe(*Subscription, UnsubscribeEvent)
}

UnsubscribeHandler is a function to handle unsubscribe event.

Directories

Path Synopsis
Simple Go chat client for https://github.com/centrifugal/centrifuge/tree/master/examples/events example.
Simple Go chat client for https://github.com/centrifugal/centrifuge/tree/master/examples/events example.
benchmark
This code is an adapted version of Nats benchmarking suite from https://github.com/nats-io/go-nats/blob/master/examples/nats-bench.go for Centrifuge.
This code is an adapted version of Nats benchmarking suite from https://github.com/nats-io/go-nats/blob/master/examples/nats-bench.go for Centrifuge.
jwt
Private channel subscription example.
Private channel subscription example.
internal
proto
Package proto is a generated protocol buffer package.
Package proto is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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