Documentation
¶
Index ¶
- func Encode(msg *ServerMessage) ([]byte, error)
- func MatchAny(patterns []string, topic string) bool
- func MatchTopic(pattern, topic string) bool
- type Message
- type MessageType
- type ServerMessage
- func NewAuthSuccessMsg(requestID string) *ServerMessage
- func NewErrorMsg(requestID, errText string) *ServerMessage
- func NewEventMsg(topic string, payload json.RawMessage) *ServerMessage
- func NewPongMsg(requestID string) *ServerMessage
- func NewSubscribedMsg(requestID, topic string) *ServerMessage
- func NewUnsubscribedMsg(requestID, topic string) *ServerMessage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Encode ¶
func Encode(msg *ServerMessage) ([]byte, error)
Encode serializes a ServerMessage to JSON bytes.
func MatchTopic ¶
MatchTopic checks if a topic matches a pattern. Patterns support:
- exact match: "user.created" matches "user.created"
- wildcard segment: "user.*" matches "user.created", "user.deleted"
- multi-segment wildcard: "**" matches everything
Segments are separated by ".".
Types ¶
type Message ¶
type Message struct {
ID string `json:"id"`
Type MessageType `json:"type"`
Topic string `json:"topic,omitempty"`
Payload json.RawMessage `json:"payload,omitempty"`
Timestamp time.Time `json:"timestamp"`
}
Message represents a client-to-server WebSocket message.
type MessageType ¶
type MessageType string
MessageType represents the type of a WebSocket message.
const ( TypeSubscribe MessageType = "subscribe" TypePublish MessageType = "publish" TypeUnsubscribe MessageType = "unsubscribe" TypePing MessageType = "ping" TypeAuth MessageType = "auth" )
Client-to-server message types.
const ( TypeSubscribed MessageType = "subscribed" TypeUnsubscribed MessageType = "unsubscribed" TypeMessage MessageType = "message" TypeError MessageType = "error" TypePong MessageType = "pong" TypeAuthSuccess MessageType = "auth_success" )
Server-to-client message types.
type ServerMessage ¶
type ServerMessage struct {
ID string `json:"id"`
Type MessageType `json:"type"`
Topic string `json:"topic,omitempty"`
Payload json.RawMessage `json:"payload,omitempty"`
Timestamp time.Time `json:"timestamp"`
}
ServerMessage represents a server-to-client WebSocket message.
func NewAuthSuccessMsg ¶
func NewAuthSuccessMsg(requestID string) *ServerMessage
NewAuthSuccessMsg creates an auth success response message.
func NewErrorMsg ¶
func NewErrorMsg(requestID, errText string) *ServerMessage
NewErrorMsg creates an error response message.
func NewEventMsg ¶
func NewEventMsg(topic string, payload json.RawMessage) *ServerMessage
NewEventMsg creates a server-to-client event message.
func NewPongMsg ¶
func NewPongMsg(requestID string) *ServerMessage
NewPongMsg creates a pong response message.
func NewSubscribedMsg ¶
func NewSubscribedMsg(requestID, topic string) *ServerMessage
NewSubscribedMsg creates a subscription confirmation message.
func NewUnsubscribedMsg ¶
func NewUnsubscribedMsg(requestID, topic string) *ServerMessage
NewUnsubscribedMsg creates an unsubscription confirmation message.