Documentation
¶
Overview ¶
Package ws contains several tools for dealing with websockets such as easily setting these up error handling.
Index ¶
- func ErrorResponse(ctx context.Context, conn *websocket.Conn, status int, message any)
- func FailedValidationResponse(ctx context.Context, conn *websocket.Conn, errors map[string]string)
- func ForbiddenResponse(ctx context.Context, conn *websocket.Conn)
- func ServerErrorResponse(ctx context.Context, conn *websocket.Conn, err error)
- func UpgradeErrorResponse(w http.ResponseWriter, r *http.Request, err error)
- type OnSubscribeCallback
- type SubscribeMessageDto
- type Subscriber
- type Topic
- type WebSocketHandler
- func (h *WebSocketHandler[T]) AddTopic(topicName string, allowedOrigins []string, ...) (*Topic, error)
- func (h WebSocketHandler[T]) Handler() http.HandlerFunc
- func (h *WebSocketHandler[T]) RemoveTopic(topic *Topic) error
- func (h *WebSocketHandler[T]) UpdateTopicName(topic *Topic, newName string) (*Topic, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorResponse ¶
ErrorResponse is used to handle any kind of error that occurred on a WebSocket.
func FailedValidationResponse ¶
func FailedValidationResponse( ctx context.Context, conn *websocket.Conn, errors map[string]string, )
FailedValidationResponse is used to handle an error of a validate.Validator that occurred on a WebSocket.
func ForbiddenResponse ¶
ForbiddenResponse is used to handle an error when a user isn't authorized to access a certain resource.
func ServerErrorResponse ¶
ServerErrorResponse is used to handle internal server errors that occurred on a WebSocket.
func UpgradeErrorResponse ¶
func UpgradeErrorResponse(w http.ResponseWriter, r *http.Request, err error)
UpgradeErrorResponse is used to handle an error that occurred during the upgrade towards a WebSocket.
Types ¶
type OnSubscribeCallback ¶
OnSubscribeCallback is called to fetch data that should be returned when a new subscriber is added to a topic.
type SubscribeMessageDto ¶
type SubscribeMessageDto interface {
validate.ValidatedType
Topic() string
}
SubscribeMessageDto is implemented by all messages used to subscribe to a certain handler of a WebSocketHandler.
type Subscriber ¶
type Subscriber struct {
// contains filtered or unexported fields
}
Subscriber is used to receive messages from a Topic and managed the websocket.Conn.
func NewSubscriber ¶
func NewSubscriber(topic *Topic, conn *websocket.Conn) Subscriber
NewSubscriber returns a new Subscriber.
func (Subscriber) OnEventCallback ¶
func (sub Subscriber) OnEventCallback(event any)
OnEventCallback is called when a new event is pushed to Subscriber. If the connection would be closed, [UnSubscribe] will be called.
type Topic ¶
type Topic struct {
Name string
// contains filtered or unexported fields
}
Topic is used to efficiently send messages to [Subscriber]s in a WebSocket.
func NewTopic ¶
func NewTopic( logger *slog.Logger, name string, allowedOrigins []string, maxWorkers int, channelBufferSize int, onSubscribeCallback OnSubscribeCallback, ) *Topic
NewTopic creates a new Topic.
func (*Topic) EnqueueEvent ¶
EnqueueEvent enqueues an event if there are subscribers on this Topic.
func (*Topic) Subscribe ¶
Subscribe subscribes a Subscriber to this Topic. If configured a message will be sent on subscribing. If no message handling go routine was running this will be started now.
func (*Topic) UnSubscribe ¶
func (t *Topic) UnSubscribe(sub Subscriber)
UnSubscribe unsubscribes a Subscriber from this Topic.
type WebSocketHandler ¶
type WebSocketHandler[T SubscribeMessageDto] struct { // contains filtered or unexported fields }
A WebSocketHandler handles incoming requests to a websocket and makes sure subscriptions are made to the right topics.
func CreateWebSocketHandler ¶
func CreateWebSocketHandler[T SubscribeMessageDto]( logger *slog.Logger, maxTopicWorkers int, topicChannelBufferSize int, ) WebSocketHandler[T]
CreateWebSocketHandler creates a new WebSocketHandler.
func (*WebSocketHandler[T]) AddTopic ¶
func (h *WebSocketHandler[T]) AddTopic( topicName string, allowedOrigins []string, onSubscribeCallback OnSubscribeCallback, ) (*Topic, error)
AddTopic adds a topic to which can be subscribed using a SubscribeMessageDto. The onSubscribeCallback is called for each new subscriber to fetch data to send them back.
func (WebSocketHandler[T]) Handler ¶
func (h WebSocketHandler[T]) Handler() http.HandlerFunc
Handler returns the http.HandlerFunc of a WebSocketHandler.
func (*WebSocketHandler[T]) RemoveTopic ¶
func (h *WebSocketHandler[T]) RemoveTopic(topic *Topic) error
RemoveTopic removes a topic to which can be subscribed using a SubscribeMessageDto.
func (*WebSocketHandler[T]) UpdateTopicName ¶
func (h *WebSocketHandler[T]) UpdateTopicName( topic *Topic, newName string, ) (*Topic, error)
UpdateTopicName updates the name of a topic without losing its subscribers.