Documentation
¶
Index ¶
- Constants
- Variables
- type Client
- type ClientErrorHandlerFunc
- type ClientErrorMsgHandlerFunc
- type ClientPublisher
- type ErrorDetail
- type ErrorMessage
- type HandlerFunc
- type Message
- func (m Message) Bind(v interface{}) error
- func (m *Message) ChannelID() string
- func (m *Message) ID() string
- func (m *Message) Key() string
- func (m *Message) MarshalJSON() ([]byte, error)
- func (m *Message) NewFrom(key string) *Message
- func (m *Message) NoContent() (*Message, error)
- func (m *Message) Timestamp() time.Time
- func (m *Message) ToError(err interface{}) *ErrorMessage
- func (m *Message) UnmarshalJSON(bb []byte) error
- func (m *Message) WithBody(v interface{}) error
- type MiddlewareFunc
- type Publisher
- type Request
- type ServerChannelBroadcaster
- type ServerDirectBroadcaster
- type ServerErrorHandlerFunc
Constants ¶
const ( MessageJoinSuccess = "join.success" MessageLeaveSuccess = "leave.success" MessageGetInfo = "get.info" MessageInfo = "info" MessageError = "error" MessageChannelExpired = "channel.expired" MessageChannelClosed = "channel.closed" )
Common messages.
const (
HeaderChannelExpiry = "X-Channel-Expires"
)
common headers.
Variables ¶
var ErrChannelNotFound = errors.New("channel not found")
ErrChannelNotFound returned when channel is not found.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client interface {
JoinChannel(host, channelID string, headers http.Header, params map[string]string) error
LeaveChannel(channelID string, headers http.Header)
RegisterListener(msgType string, fn HandlerFunc)
}
Client can be used to implement a client which will send and listen to messages on channels.
type ClientErrorHandlerFunc ¶
ClientErrorHandlerFunc is raised when the client itself returns an error when processing a message. It can be logged, retried etc.
type ClientErrorMsgHandlerFunc ¶
type ClientErrorMsgHandlerFunc func(err ErrorMessage)
ClientErrorMsgHandlerFunc is triggered when a server returns an error message in response to a client message. It will contain the detail of the error and the client can decide to log, re-send or do something else.
type ClientPublisher ¶
type ClientPublisher interface {
Publish(req Request)
}
ClientPublisher can be implemented to allow a client to send messages to a connected server and channel.
type ErrorDetail ¶
ErrorDetail is returned as the message body in the event of an error.
type ErrorMessage ¶
type ErrorMessage struct {
CorrelationID string `json:"correlationId"`
AppID string `json:"appId"`
UserID string `json:"userId"`
Key string `json:"type"`
OriginKey string `json:"originType"`
OriginBody json.RawMessage `json:"originBody"`
ErrorBody json.RawMessage `json:"errorBody"`
ChannelID string `json:"channelId"`
ClientID string `json:"clientId"`
Headers http.Header `json:"headers"`
}
ErrorMessage is a message type returned on error, it contains a copy of the original key and body but will have a key of "error" allowing this to be checked for in error handlers. ErrorBody is optional but if supplied can be marhsaled to a struct using Bind() to get the detail of the error.
func (*ErrorMessage) Bind ¶
func (e *ErrorMessage) Bind(v interface{}) error
Bind will decode the error message body to v. This message will usually contain further error data and can be specified by the server.
func (*ErrorMessage) BindOriginBody ¶
func (e *ErrorMessage) BindOriginBody(v interface{}) error
BindOriginBody can be used to decode the body for the original message that triggered this error, useful for replaying.
type HandlerFunc ¶
HandlerFunc defines listeners on both the server and clients. When registered these will be triggered when a message is received matching the key.
type Message ¶
type Message struct {
CorrelationID string
AppID string
UserID string
Expiration *time.Time
Body json.RawMessage
Headers http.Header
ClientID string
// contains filtered or unexported fields
}
Message is the underlying message type used by the protocol to transmit metadata and the message bodies.
func NewMessage ¶
NewMessage will create a new message setting.
func (*Message) MarshalJSON ¶
MarshalJSON implements the json Marshaller.
func (*Message) NewFrom ¶
NewFrom will take a copy of msg and return a new message from it.
You can then add a body using the WithBody func and add headers etc.
func (*Message) NoContent ¶
NoContent is a helper that can be used to return an empty message from a listener.
func (*Message) ToError ¶
func (m *Message) ToError(err interface{}) *ErrorMessage
ToError will convert a message to an ErrorMessage with an optional err struct supplied containing additional details for the error.
There is a default sockets.ErrorDetail struct available, or you can define your own.
func (*Message) UnmarshalJSON ¶
UnmarshalJSON implements the json unmarshaler.
type MiddlewareFunc ¶
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
MiddlewareFunc defines a common middleware signature that can be used to create middleware that executes before the HandlerFunc.
type ServerChannelBroadcaster ¶
type ServerChannelBroadcaster interface {
// Broadcast will send a message to all connected peers on a channel.
// To handle these messages you should register a handler using server.RegisterChannelHandler.
Broadcast(channelID string, msg *Message)
// BroadcastAwait will broadcast a message to all peers but handle only the first one and return
// it to the caller. This is a blocking call.
BroadcastAwait(ctx context.Context, channelID string, msg *Message) (*Message, error)
}
ServerChannelBroadcaster is used to send a message from a server to all clients connected to a channel.
type ServerDirectBroadcaster ¶
ServerDirectBroadcaster is used to send a message from a server directly to a client.
type ServerErrorHandlerFunc ¶
type ServerErrorHandlerFunc func(msg *Message, e error) *ErrorMessage
ServerErrorHandlerFunc defines an error handler for a server.
The message can be handled, logged, and returned to clients.
