Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WriteHTTPError ¶
func WriteHTTPError(w http.ResponseWriter, statusCode int, message string) error
WriteHTTPError can be used to write a HTTP error response with structure that matches the one used by the websocket.
Types ¶
type Command ¶
type Command[RequestT any, ReplyT any] struct { CommandMeta Request RequestT `json:"request"` // contains filtered or unexported fields }
Command is used in tandem with CommandPalette to define your commands
func (Command[RequestT, ReplyT]) Err ¶
Err sends an error response, taking care of setting the correct ID and Command values.
func (Command[RequestT, ReplyT]) FromRaw ¶
func (c Command[RequestT, ReplyT]) FromRaw(r RawCommand) (Errable, error)
FromRaw tries to create a command with the same type as the receiver, by decoding a RawCommand struct. This is mostly an implementation detail which makes it possible to instantiate commands with the correct types, based on a CommandPalette.
type CommandDecoder ¶
type CommandDecoder struct {
// contains filtered or unexported fields
}
CommandDecoder handles decoding the commands.
func (*CommandDecoder) Decode ¶
func (c *CommandDecoder) Decode() (Errable, error)
Decode reads and decodes a single command from the websocket in a blocking manner. Decode can be called safely from multiple threads at once.
type CommandMeta ¶
CommandMeta describes command metadata - request ID and the type name
type CommandPalette ¶
type CommandPalette map[string]fromRawable
CommandPalette maps type names to actual Command types. Use the zero value of your command types as the values.
type Conn ¶
type Conn struct { CommandDecoder // contains filtered or unexported fields }
func NewConn ¶
func NewConn(w http.ResponseWriter, r *http.Request, palette CommandPalette) (*Conn, error)
NewConn upgrades the connection to a WebSocket and returns a *Conn which can be used for further communication. The palette argument is used to register command types that the server is supposed to understand.
func (*Conn) Pump ¶ added in v0.2.0
Pump starts a pump goroutine that decodes the messages into a Go channel and returns an object wrapping the channel. See: the Pump interface.
The goroutine stops once the context expires/gets cancelled.
Useful when one needs to read from multiple sources using a select{} statement.
func (*Conn) SendMessage ¶
SendMessage pushes a message (any type implementing the Message interface) to the client. SendMessage can be used from multiple threads simultaneously.
type Message ¶
type Message interface {
Type() string
}
Message needs to be implemented by any type that the server wants to push to the client.
type Pump ¶ added in v0.2.0
type Pump interface { // Ch returns the channel to which the commands get pumped. If an error is // encountered during decoding, the channel is closed and Pump.Err() returns // the error. Ch() <-chan Errable // Err returns the error encountered when decoding, if any. Err() error }
Pump represents a pump which pumps commands from the websocket to a Go channel.
type RawCommand ¶
type RawCommand = Command[json.RawMessage, any]
RawCommand's request won't be decoded, and it's response can be anything