Documentation ¶
Index ¶
- Constants
- Variables
- func ClientJsHandler(r *chain.Router, route string)
- type Channel
- func (c *Channel) Broadcast(topic string, event string, payload any) (err error)
- func (c *Channel) Dispatch(topic string, msg any, from string)
- func (c *Channel) HandleIn(event string, handler InHandler)
- func (c *Channel) HandleOut(event string, handler OutHandler)
- func (c *Channel) Join(topic string, handler JoinHandler)
- func (c *Channel) Leave(topic string, handler LeaveHandler)
- func (c *Channel) LocalBroadcast(topic string, event string, payload any) (err error)
- type ConfigHandler
- type ConnectHandler
- type Handler
- type InHandler
- type JoinHandler
- type LeaveHandler
- type LeaveReason
- type Message
- type MessageSerializer
- type MessageType
- type OutHandler
- type Session
- type Socket
- func (s *Socket) Broadcast(event string, payload any) (err error)
- func (s *Socket) Endpoint() string
- func (s *Socket) Get(key string) (value any)
- func (s *Socket) Id() string
- func (s *Socket) Push(event string, payload any) (err error)
- func (s *Socket) Send(bytes []byte) error
- func (s *Socket) Session() *Session
- func (s *Socket) Set(key string, value any)
- func (s *Socket) Status() Status
- func (s *Socket) Topic() string
- type Status
- type Transport
- type TransportSSE
Constants ¶
const ( LeaveReasonLeave = LeaveReason(0) // Client called _leave event (channel.leave()). LeaveReasonRejoin = LeaveReason(1) // Client called _join and there is already an active socket for the same topic LeaveReasonClose = LeaveReason(2) // Connection lost and session is terminated. See Session.ScheduleShutdown )
const ( ReplyStatusCodeOk = 0 ReplyStatusCodeError = 1 MessageTypePush = MessageType(0) MessageTypeReply = MessageType(1) // Defines a reply Message sent from channels to Transport. MessageTypeBroadcast = MessageType(2) // Defines a Message sent from pubsub to channels and vice-versa. )
const ( StatusJoining = Status(0) StatusJoined = Status(1) StatusLeaving = Status(2) StatusRemoved = Status(3) )
Variables ¶
var ( ErrJoinCrashed = fmt.Errorf("join crashed") ErrUnmatchedTopic = fmt.Errorf("unmatched topic") )
var (
ErrSocketNotJoined = fmt.Errorf("socket not joined")
)
Functions ¶
func ClientJsHandler ¶
ClientJsHandler add "/chain.js" endpoint
Types ¶
type Channel ¶
type Channel struct { TopicPattern string // The string pattern, for example `"room:*"`, `"users:*"`, or `"system"` // contains filtered or unexported fields }
Channel provide a means for bidirectional communication from clients that integrate with the pubsub layer for soft-realtime functionality.
func NewChannel ¶
NewChannel Defines a channel matching the given topic.
func (*Channel) HandleIn ¶
HandleIn Handle incoming `event`s.
## Example
channel.HandleIn("current_rank", func(event string, payload any, socket *Socket) (reply any, err error) { // client asks for their current rank, push sent directly as a new event. socket.Push("current_rank", map[string]any{"val": game.GetRank(socket.Get("user"))}) return })
func (*Channel) HandleOut ¶
func (c *Channel) HandleOut(event string, handler OutHandler)
HandleOut Intercepts outgoing `event`s.
By default, broadcasted events are pushed directly to the client, but intercepting events gives your channel a chance to customize the event for the client to append extra information or filter the message from being delivered.
*Note*: intercepting events can introduce significantly more overhead if a large number of subscribers must customize a message since the broadcast will be encoded N times instead of a single shared encoding across all subscribers.
## Example
channel.HandleOut("new_msg", func(event string, payload any, socket *Socket) { if obj, valid := payload.(map[string]any); valid { obj["is_editable"] = User.CanEditMessage(socket.Get("user"), obj) socket.Push("new_msg", obj) } })
func (*Channel) Join ¶
func (c *Channel) Join(topic string, handler JoinHandler)
Join Handle channel joins by `topic`.
To authorize a socket, return `nil, nil` or `SOME_REPLY_PAYLOAD, nil`.
To refuse authorization, return `nil, reason`.
Example
channel.Join("room:lobby", func Join(payload any, socket *Socket) (reply any, err error) if !authorized(payload) { err = errors.New("unauthorized") } return })
func (*Channel) Leave ¶
func (c *Channel) Leave(topic string, handler LeaveHandler)
Leave Invoked when the socket is about to leave a Channel. See LeaveHandler
type ConfigHandler ¶
type ConnectHandler ¶
type Handler ¶
type Handler struct { Options map[string]any // Permite receber opções que estrão acessíveis Channels []*Channel // Channels in this socket Transports []Transport // Configured Transports Serializer chain.Serializer // Serializer definido para o Transport OnConfig ConfigHandler // Called by Handler.Configure OnConnect ConnectHandler // Called when client try to connect on a Transport // contains filtered or unexported fields }
Handler A socket implementation that multiplexes messages over channels.
Handler is used as a module for establishing and maintaining the socket state via the Session and Socket struct.
Once connected to a socket, incoming and outgoing events are routed to Channel. The incoming client data is routed to channels via transports. It is the responsibility of the Handler to tie Transport and Channel together.
type InHandler ¶
InHandler invoked when the client push an event to a channel (`js: channel.push(event, payload)`).
See Channel.HandleIn
type JoinHandler ¶
JoinHandler invoked when the client joins a channel (event:_join, `js: channel.join()`).
See Channel.Join
type LeaveHandler ¶
type LeaveHandler func(socket *Socket, reason LeaveReason)
LeaveHandler invoked when the socket leave a channel.
See LeaveReason, Channel.Leave
type Message ¶
type Message struct { Kind MessageType `json:"k,omitempty"` // Type of message JoinRef int `json:"j,omitempty"` // The unique number ref when joining Ref int `json:"r,omitempty"` // The unique number ref Status int `json:"s,omitempty"` // The reply status Topic string `json:"t,omitempty"` // The string topic or topic:subtopic pair namespace, for example "messages", "messages:123" Event string `json:"e,omitempty"` // The string event name, for example "_join" Payload any `json:"p,omitempty"` // The Message payload }
Message Defines a message dispatched over transport to channels and vice-versa.
type MessageSerializer ¶
type MessageSerializer struct{}
type MessageType ¶
type MessageType int
type Session ¶
type Session struct { Params map[string]string // Initialization parameters, received at connection time Options map[string]any // Reference to Handler.Options // contains filtered or unexported fields }
Session used by Transport, communication interface between Transport and Channel.
Keeps an active session on the server. Transport should invoke ScheduleShutdown method when user connection drops
func (*Session) ScheduleShutdown ¶
ScheduleShutdown schedules the termination of this session on the server. In case of a user reconnection, invoke the StopScheduledShutdown or Handler.Resume methods
func (*Session) StopScheduledShutdown ¶
func (s *Session) StopScheduledShutdown()
StopScheduledShutdown cancels the final termination of that session.
Invoked by the Handler.Resume method
type Socket ¶
type Socket struct { Params map[string]string // Initialization parameters, received at connection time. // contains filtered or unexported fields }
Socket Channel integration.
Allows the channel to manage socket state data through the Socket.Set and Socket.Get
type TransportSSE ¶
type TransportSSE struct {
// contains filtered or unexported fields
}