Documentation
¶
Index ¶
- Variables
- func LogError(err IError)
- func NewConnection(upgrader websocket.Upgrader, w http.ResponseWriter, r *http.Request) (*Connection, IError)
- type Broker
- func (broker *Broker) ChannelName(namespace string, room string) string
- func (broker *Broker) ID() string
- func (broker *Broker) IsSubscribed(namespace string, room string) bool
- func (broker *Broker) OnMessage(channel *Channel, data *Packet) error
- func (broker *Broker) OnPublish(channel *Channel, data *Packet) error
- func (broker *Broker) OnSubscribe(channel *Channel, done chan<- IError)
- func (broker *Broker) OnUnsubscribe(channel *Channel, done chan<- IError)
- func (broker *Broker) Store() cmap.ConcurrentMap
- type Channel
- type ChannelEvent
- type ChannelModel
- func (m *ChannelModel) Disconnect(room string, connection *Connection) IError
- func (m *ChannelModel) Namespace() string
- func (m *ChannelModel) Receive(event string, message string) (string, IError)
- func (m *ChannelModel) Subscribe(room string, message *Message, connection *Connection) (bool, IError)
- type ChannelState
- type ClientError
- type Connection
- type Error
- type EventModel
- type IBroker
- type IChannels
- type IError
- type IEvents
- type Message
- type Option
- type Packet
- type PacketError
- type PacketType
- type ProtocolError
- type RedisBroker
- func (broker *RedisBroker) OnMessage(channel *Channel, data *Packet) error
- func (broker *RedisBroker) OnPublish(channel *Channel, data *Packet) error
- func (broker *RedisBroker) OnSubscribe(channel *Channel, done chan<- IError)
- func (broker *RedisBroker) OnUnsubscribe(channel *Channel, done chan<- IError)
- type SimpleBroker
- func (broker *SimpleBroker) OnMessage(channel *Channel, data *Packet) error
- func (broker *SimpleBroker) OnPublish(channel *Channel, data *Packet) error
- func (broker *SimpleBroker) OnSubscribe(channel *Channel, done chan<- IError)
- func (broker *SimpleBroker) OnUnsubscribe(channel *Channel, done chan<- IError)
- type Sphere
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = &ProtocolError{"not found"} ErrNotSupported = &ProtocolError{"not supported"} ErrNotImplemented = &ProtocolError{"not implemented"} ErrTooManyRequest = &ProtocolError{"too many requests"} ErrBadScheme = &ProtocolError{"bad scheme"} ErrBadStatus = &ProtocolError{"bad status"} ErrBadRequestMethod = &ProtocolError{"bad method"} ErrServerErrors = &ProtocolError{"server errors"} ErrRequestFailed = &ProtocolError{"request failed"} ErrAlreadySubscribed = &ClientError{"already subscribed"} ErrNotSubscribed = &ClientError{"not subscribed"} ErrPacketBadScheme = &PacketError{"packet bad scheme"} ErrPacketBadType = &PacketError{"packet bad type"} )
List of errors
var ChannelEventCode = [...]string{
"subscribe",
"unsubscribe",
"subscribeFail",
}
ChannelEventCode returns the string value of ChannelEvent
var ChannelStateCode = [...]string{
"subscribed",
"unsubscribed",
"pending",
}
ChannelStateCode returns the string value of ChannelState
var PacketTypeCode = [...]string{
"message",
"channel",
"subscribe",
"unsubscribe",
"subscribed",
"unsubscribed",
"ping",
"pong",
"unknown",
}
PacketTypeCode returns the string value of SphereError
Functions ¶
func NewConnection ¶
func NewConnection(upgrader websocket.Upgrader, w http.ResponseWriter, r *http.Request) (*Connection, IError)
NewConnection returns a new ws connection instance
Types ¶
type Broker ¶
type Broker struct {
// contains filtered or unexported fields
}
Broker allows you to interact directly with Websocket internal data and pub/sub channels
func (*Broker) ChannelName ¶
ChannelName returns channel name with provided namespace and room name
func (*Broker) IsSubscribed ¶
IsSubscribed return the broker state of the channel
func (*Broker) OnPublish ¶
OnPublish when websocket publishes data to a particular channel from the current broker
func (*Broker) OnSubscribe ¶
OnSubscribe when websocket subscribes to a channel
func (*Broker) OnUnsubscribe ¶
OnUnsubscribe when websocket unsubscribes from a channel
type Channel ¶
type Channel struct {
// contains filtered or unexported fields
}
Channel let you subscribe to and watch for incoming data which is published on that channel by other clients or the server
func NewChannel ¶
NewChannel creates new Channel instance
func (*Channel) Connections ¶
func (channel *Channel) Connections() []*Connection
Connections returns a list of active user connections
func (*Channel) Emit ¶
func (channel *Channel) Emit(mt int, payload []byte, c *Connection) IError
Emit sends message to current channel
func (*Channel) State ¶
func (channel *Channel) State() ChannelState
State returns the state of the channel
type ChannelEvent ¶
type ChannelEvent int
ChannelEvent indicates the state of the channel
const ( // ChannelEventSubscribe indicates that this channel is in a subscribed state ChannelEventSubscribe ChannelEvent = iota // ChannelEventUnsubscribe indicates that this channel is in a unsubscribed state ChannelEventUnsubscribe // ChannelEventSubscribeFail indicates that this channel is in a pending state ChannelEventSubscribeFail )
func (ChannelEvent) String ¶
func (s ChannelEvent) String() string
Returns the code id of channel state
type ChannelModel ¶
type ChannelModel struct {
// contains filtered or unexported fields
}
ChannelModel is for user to define channel events and actions
func ExtendChannelModel ¶
func ExtendChannelModel(namespace string) *ChannelModel
ExtendChannelModel lets developer create a IChannals compatible struct
func (*ChannelModel) Disconnect ¶
func (m *ChannelModel) Disconnect(room string, connection *Connection) IError
Disconnect defines the action when user disconnect from channel
func (*ChannelModel) Namespace ¶
func (m *ChannelModel) Namespace() string
Namespace to return name of the channel
func (*ChannelModel) Receive ¶
func (m *ChannelModel) Receive(event string, message string) (string, IError)
Receive defines the action when websocket server receive message from user in this channel
func (*ChannelModel) Subscribe ¶
func (m *ChannelModel) Subscribe(room string, message *Message, connection *Connection) (bool, IError)
Subscribe decides whether accept the connection into channel or not, return true => accept, false => reject
type ChannelState ¶
type ChannelState int
ChannelState indicates the state of the channel
const ( // ChannelStateSubscribed indicates that this channel is in a subscribed state ChannelStateSubscribed ChannelState = iota // ChannelStateUnsubscribed indicates that this channel is in a unsubscribed state ChannelStateUnsubscribed // ChannelStatePending indicates that this channel is in a pending state ChannelStatePending )
func (ChannelState) String ¶
func (s ChannelState) String() string
Returns the code id of channel state
type ClientError ¶
type ClientError Error
ClientError represents general client errors.
func (*ClientError) Error ¶
func (e *ClientError) Error() string
Error returns error string of ChannelError
type Connection ¶
type Connection struct { // websocket connection *websocket.Conn // contains filtered or unexported fields }
Connection allows you to interact with backend and other client sockets in realtime
func (*Connection) Cookies ¶
func (conn *Connection) Cookies() []*http.Cookie
Cookies export connection cookies
func (*Connection) Headers ¶
func (conn *Connection) Headers() http.Header
Headers export connection headers
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is a trivial implementation of error.
type EventModel ¶
type EventModel struct {
// contains filtered or unexported fields
}
EventModel is for user to define channel events and actions
func ExtendEventModel ¶
func ExtendEventModel(namespace string) *EventModel
ExtendEventModel lets developer create a IEvents compatible struct
func (*EventModel) Namespace ¶
func (m *EventModel) Namespace() string
Namespace to return name of the channel
type IBroker ¶
type IBroker interface { ID() string // => Broker ID ChannelName(string, string) string // => Broker generate channel name with namespace and channel IsSubscribed(string, string) bool // => Broker channel subscribe state OnSubscribe(*Channel, chan<- IError) // => Broker OnSubscribe OnUnsubscribe(*Channel, chan<- IError) // => Broker OnUnsubscribe OnPublish(*Channel, *Packet) error // => Broker OnPublish OnMessage(*Channel, *Packet) error // => Broker OnMessage }
IBroker represents Broker instance
type IChannels ¶
type IChannels interface { Namespace() string Subscribe(string, *Message, *Connection) (bool, IError) Disconnect(string, *Connection) IError Receive(string, string) (string, IError) }
IChannels is the interface for ChannelModel
type Packet ¶
type Packet struct { Type PacketType `json:"type"` Namespace string `json:"namespace,omitempty"` Room string `json:"room,omitempty"` Cid int `json:"cid"` Error error `json:"error,omitempty"` Message *Message `json:"message,omitempty"` Reply bool `json:"reply"` Machine string `json:"-"` }
Packet indicates the data of the message
func ParsePacket ¶
ParsePacket returns Packet from bytes
type PacketError ¶
type PacketError Error
PacketError represents general client errors.
func (*PacketError) Error ¶
func (e *PacketError) Error() string
Error returns error string of ChannelError
type PacketType ¶
type PacketType int
PacketType indicates the error of the channel
const ( // PacketTypeMessage denotes a regular text data message. PacketTypeMessage PacketType = iota // PacketTypeChannel denotes a channel request. PacketTypeChannel // PacketTypeSubscribe denotes a subscribe request. PacketTypeSubscribe // PacketTypeUnsubscribe denotes an unsubscribe request. PacketTypeUnsubscribe // PacketTypeSubscribed denotes a response of subscribe request. PacketTypeSubscribed // PacketTypeUnsubscribed denotes a response of unsubscribe request. PacketTypeUnsubscribed // PacketTypePing denotes an ping message. PacketTypePing // PacketTypePong denotes an pong message. PacketTypePong // PacketTypeUnknown denotes an pong message. PacketTypeUnknown )
func (*PacketType) MarshalJSON ¶
func (p *PacketType) MarshalJSON() ([]byte, error)
MarshalJSON to convert PacketType to json string
func (*PacketType) UnmarshalJSON ¶
func (p *PacketType) UnmarshalJSON(b []byte) (err error)
UnmarshalJSON to parse object from json string
type ProtocolError ¶
type ProtocolError Error
ProtocolError represents WebSocket protocol errors.
func (*ProtocolError) Error ¶
func (e *ProtocolError) Error() string
Error returns error string of ProtocolError
type RedisBroker ¶
type RedisBroker struct {
*Broker
}
RedisBroker is a broker adapter built on Redis client
func DefaultRedisBroker ¶
func DefaultRedisBroker() *RedisBroker
DefaultRedisBroker creates a new instance of RedisBroker
func (*RedisBroker) OnMessage ¶
func (broker *RedisBroker) OnMessage(channel *Channel, data *Packet) error
OnMessage when websocket receive data from the broker subscriber
func (*RedisBroker) OnPublish ¶
func (broker *RedisBroker) OnPublish(channel *Channel, data *Packet) error
OnPublish when websocket publishes data to a particular channel from the current broker
func (*RedisBroker) OnSubscribe ¶
func (broker *RedisBroker) OnSubscribe(channel *Channel, done chan<- IError)
OnSubscribe when websocket subscribes to a channel
func (*RedisBroker) OnUnsubscribe ¶
func (broker *RedisBroker) OnUnsubscribe(channel *Channel, done chan<- IError)
OnUnsubscribe when websocket unsubscribes from a channel
type SimpleBroker ¶
type SimpleBroker struct {
*Broker
}
SimpleBroker is a broker adapter built on Simple client
func DefaultSimpleBroker ¶
func DefaultSimpleBroker() *SimpleBroker
DefaultSimpleBroker creates a new instance of SimpleBroker
func (*SimpleBroker) OnMessage ¶
func (broker *SimpleBroker) OnMessage(channel *Channel, data *Packet) error
OnMessage when websocket receive data from the broker subscriber
func (*SimpleBroker) OnPublish ¶
func (broker *SimpleBroker) OnPublish(channel *Channel, data *Packet) error
OnPublish when websocket publishes data to a particular channel from the current broker
func (*SimpleBroker) OnSubscribe ¶
func (broker *SimpleBroker) OnSubscribe(channel *Channel, done chan<- IError)
OnSubscribe when websocket subscribes to a channel
func (*SimpleBroker) OnUnsubscribe ¶
func (broker *SimpleBroker) OnUnsubscribe(channel *Channel, done chan<- IError)
OnUnsubscribe when websocket unsubscribes from a channel