Documentation
¶
Overview ¶
Package websocket can connect to a WebSocket server from within a CLI app.
Index ¶
- Constants
- Variables
- func Do(ctx context.Context) error
- func SourceAnnotation() (string, string)
- type Action
- func ConnectAndPrint() Action
- func ContextValue(c *Client) Action
- func FlagsAndArgs() Action
- func SetBinary() Action
- func SetCloseTimeout(d ...time.Duration) Action
- func SetCompression(v ...bool) Action
- func SetHandshakeTimeout(d ...time.Duration) Action
- func SetHeader(v ...*httpclient.HeaderValue) Action
- func SetInput(v ...*cli.FileSet) Action
- func SetMessage(v ...string) Action
- func SetMessageType(v ...MessageType) Action
- func SetOrigin(v ...string) Action
- func SetReadBufferSize(v ...int) Action
- func SetReadLimit(v ...int64) Action
- func SetReadTimeout(d ...time.Duration) Action
- func SetSubprotocol(v ...string) Action
- func SetURLValue(u ...*URLValue) Action
- func SetVerbose() Action
- func SetWriteBufferSize(v ...int) Action
- func SetWriteTimeout(d ...time.Duration) Action
- type Client
- func (c *Client) Apply(opts ...Option)
- func (c *Client) Dial(ctx context.Context) (*Conn, error)
- func (c *Client) Do(ctx context.Context) error
- func (c *Client) Location() (*URLValue, error)
- func (c *Client) NewDialer(ctx context.Context) (*ws.Dialer, error)
- func (c *Client) NewTLSConfig(ctx context.Context) (*gotls.Config, error)
- func (c *Client) Pipeline() cli.Action
- type Conn
- type MessageType
- type Option
- func WithAction(a cli.Action) Option
- func WithCloseTimeout(d time.Duration) Option
- func WithCompression(v bool) Option
- func WithDefaultAction() Option
- func WithDefaultDialerFactory() Option
- func WithDefaultTLSConfigFactory() Option
- func WithDialer(d *ws.Dialer) Option
- func WithDialerFactory(fn func(context.Context) (*ws.Dialer, error)) Option
- func WithHandshakeTimeout(d time.Duration) Option
- func WithHeader(name, value string) Option
- func WithInput(files *cli.FileSet) Option
- func WithMessage(text string) Option
- func WithMessageType(m MessageType) Option
- func WithOrigin(origin string) Option
- func WithReadBufferSize(n int) Option
- func WithReadLimit(n int64) Option
- func WithReadTimeout(d time.Duration) Option
- func WithSubprotocol(name string) Option
- func WithTLSConfig(t *gotls.Config) Option
- func WithTLSConfigFactory(fn func(context.Context) (*gotls.Config, error)) Option
- func WithURL(loc string) Option
- func WithVerbose(v bool) Option
- func WithWriteBufferSize(n int) Option
- func WithWriteTimeout(d time.Duration) Option
- type Options
- type URLValue
Constants ¶
const ( // TextMessage denotes a text data message, whose payload is interpreted as // UTF-8 encoded text TextMessage = MessageType(ws.TextMessage) // BinaryMessage denotes a binary data message BinaryMessage = MessageType(ws.BinaryMessage) )
Variables ¶
var ( // ErrNoLocation is reported when no URL has been specified for the client ErrNoLocation = errors.New("no WebSocket URL specified") )
Functions ¶
func SourceAnnotation ¶
SourceAnnotation gets the name and value of the annotation added to the Data of all flags that are initialized from this package
Types ¶
type Action ¶
type Action = cli.Action
Action is an alias for the action within the Joe framework
func ConnectAndPrint ¶
func ConnectAndPrint() Action
ConnectAndPrint provides an action which connects to the WebSocket server, sends each of the messages which have been configured, and prints each message which is received.
func ContextValue ¶
ContextValue provides an action which stores the client in the context
func FlagsAndArgs ¶
func FlagsAndArgs() Action
FlagsAndArgs adds the flags and args that can be used to configure the client in the context.
func SetCloseTimeout ¶
SetCloseTimeout sets the maximum duration to allow for the closing handshake, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetCompression ¶
SetCompression enables negotiation of per-message compression
func SetHandshakeTimeout ¶
SetHandshakeTimeout sets the maximum duration to allow the handshake to complete, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetHeader ¶
func SetHeader(v ...*httpclient.HeaderValue) Action
SetHeader adds a header to the handshake request, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetInput ¶
func SetInput(v ...*cli.FileSet) Action
SetInput sets the files which provide the messages to send, one message per line, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetMessage ¶
SetMessage adds a message which is sent once the connection is established, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetMessageType ¶
func SetMessageType(v ...MessageType) Action
SetMessageType sets the type of the messages which are sent, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetOrigin ¶
SetOrigin sets the Origin header used in the handshake request, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetReadBufferSize ¶
SetReadBufferSize sets the size of the read buffer in bytes
func SetReadLimit ¶
SetReadLimit sets the maximum size in bytes of the messages which can be received
func SetReadTimeout ¶
SetReadTimeout sets the maximum duration to wait for each message which is received, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetSubprotocol ¶
SetSubprotocol adds a subprotocol which is requested during the handshake, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetURLValue ¶
SetURLValue sets the location that the client connects to, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
func SetVerbose ¶
func SetVerbose() Action
SetVerbose causes the messages which are sent to be echoed to stderr
func SetWriteBufferSize ¶
SetWriteBufferSize sets the size of the write buffer in bytes
func SetWriteTimeout ¶
SetWriteTimeout sets the maximum duration to allow for writing each message, which either uses the specified value or reads from the corresponding flag/arg to get the value to set.
type Client ¶
type Client struct {
cli.Action
// contains filtered or unexported fields
}
Client provides a WebSocket client (which encapsulates a websocket.Dialer) that can be accessed from commands, flags, and args within Joe applications. The client is used within the Uses pipeline where it registers itself as a context service together with the flags and args that configure it. The action ConnectAndPrint is used to actually exchange messages:
&cli.App{
Name: "gows",
Uses: websocket.New(),
Action: websocket.ConnectAndPrint(),
}
This simple app has numerous flags to configure the handshake and message handling, and its simplest invocation could be something like
gows ws://example.com/graphql
The client is configured exclusively with Options, either passed to New or applied later using Apply. The underlying websocket.Dialer is created on demand the first time it is needed, which is available from NewDialer. How it gets created can be customized with WithDialer or WithDialerFactory.
The cmd/mop package provides mop, which is a command line utility very similar to this.
If you only want to add the Client to the context (typically in advanced scenarios where you are deeply customizing the behavior), you only use the action websocket.ContextValue() with the client you want to add instead of add the client to the pipeline directly.
func FromContext ¶
FromContext obtains the client stored in the context
func (*Client) Do ¶
Do establishes the connection, sends each message which has been configured, and then copies each message which is received to the output until the peer closes the connection or the read timeout elapses.
func (*Client) NewDialer ¶
NewDialer creates (or returns the cached) websocket.Dialer for the client
func (*Client) NewTLSConfig ¶
NewTLSConfig creates the TLS config
type Conn ¶
Conn represents a WebSocket connection which has been established by the client. It wraps the underlying websocket.Conn so that the timeouts and message type which were configured on the client are applied to each operation. The embedded connection can be used directly when more control is required.
func (*Conn) CloseNormal ¶
CloseNormal performs the closing handshake, indicating normal closure
func (*Conn) Receive ¶
func (c *Conn) Receive() (MessageType, []byte, error)
Receive reads the next message from the connection
type MessageType ¶
type MessageType int
MessageType identifies the type of a WebSocket message
func (MessageType) MarshalText ¶
func (m MessageType) MarshalText() ([]byte, error)
MarshalText provides the textual representation of the message type
func (*MessageType) Set ¶
func (m *MessageType) Set(arg string) error
Set updates the value from the text of the flag or arg
func (MessageType) String ¶
func (m MessageType) String() string
String obtains the name of the message type
func (MessageType) Synopsis ¶
func (MessageType) Synopsis() string
Synopsis obtains the placeholder text used in help screens
func (*MessageType) UnmarshalText ¶
func (m *MessageType) UnmarshalText(b []byte) error
UnmarshalText parses the textual representation of the message type
type Option ¶
type Option interface {
cli.Action
// contains filtered or unexported methods
}
Option is an option to configure the client. Option can be used as an Action, typically within the Uses or Before pipeline.
func WithCloseTimeout ¶
WithCloseTimeout sets the amount of time to allow for the closing handshake
func WithCompression ¶
WithCompression enables negotiation of per-message compression
func WithDefaultAction ¶
func WithDefaultAction() Option
WithDefaultAction sets the action to the default, which registers the client in the context along with the flags and args that configure it.
func WithDefaultDialerFactory ¶
func WithDefaultDialerFactory() Option
WithDefaultDialerFactory sets up the default dialer factory and the built-in dialer middleware (connection settings and TLS setup). This option is applied automatically by New.
func WithDefaultTLSConfigFactory ¶
func WithDefaultTLSConfigFactory() Option
WithDefaultTLSConfigFactory provides the default factory, which provides TLS from the context
func WithDialer ¶
WithDialer sets the websocket.Dialer to use directly, bypassing the default factory. The connection settings which have been configured with the other options are still applied to it.
func WithDialerFactory ¶
WithDialerFactory provides a factory for obtaining the websocket.Dialer.
func WithHandshakeTimeout ¶
WithHandshakeTimeout sets the amount of time to allow the handshake to complete
func WithHeader ¶
WithHeader adds a header to the handshake request. Note that the Sec-WebSocket-Protocol header is set from the subprotocols which have been configured with WithSubprotocol rather than being set directly.
func WithInput ¶
func WithInput(files *cli.FileSet) Option
WithInput sets the files that provide the messages to send, one message per line. When the file set is empty, standard input is used unless it is a terminal.
func WithMessage ¶
WithMessage adds a message which is sent once the connection is established. Messages are sent before any which are read from the input.
func WithMessageType ¶
func WithMessageType(m MessageType) Option
WithMessageType sets the type of the messages which are sent
func WithOrigin ¶
WithOrigin sets the Origin header used in the handshake request
func WithReadBufferSize ¶
WithReadBufferSize sets the size of the read buffer in bytes
func WithReadLimit ¶
WithReadLimit sets the maximum size in bytes of the messages which can be received
func WithReadTimeout ¶
WithReadTimeout sets the amount of time to wait for each message which is received. When the timeout elapses, the exchange has ended.
func WithSubprotocol ¶
WithSubprotocol adds a subprotocol which is requested during the handshake
func WithTLSConfig ¶
WithTLSConfig sets the TLS config for use on the client
func WithTLSConfigFactory ¶
WithTLSConfigFactory provides a factory for obtaining TLS config
func WithURL ¶
WithURL sets the location that the client connects to. The text is interpreted as described by URLValue.
func WithVerbose ¶
WithVerbose causes the messages which are sent to be echoed to stderr
func WithWriteBufferSize ¶
WithWriteBufferSize sets the size of the write buffer in bytes
func WithWriteTimeout ¶
WithWriteTimeout sets the amount of time to allow for writing each message
type Options ¶
type Options struct {
URL *string `toml:"url" json:"url,omitempty"`
Headers map[string]string `toml:"headers" json:"headers,omitempty"`
Origin *string `toml:"origin" json:"origin,omitempty"`
Subprotocols []string `toml:"subprotocols" json:"subprotocols,omitempty"`
Messages []string `toml:"messages" json:"messages,omitempty"`
MessageType *MessageType `toml:"message-type" json:"messageType,omitempty"`
HandshakeTimeout *time.Duration `toml:"handshake-timeout" json:"handshakeTimeout,omitempty"`
ReadTimeout *time.Duration `toml:"read-timeout" json:"readTimeout,omitempty"`
WriteTimeout *time.Duration `toml:"write-timeout" json:"writeTimeout,omitempty"`
CloseTimeout *time.Duration `toml:"close-timeout" json:"closeTimeout,omitempty"`
ReadBufferSize *int `toml:"read-buffer-size" json:"readBufferSize,omitempty"`
WriteBufferSize *int `toml:"write-buffer-size" json:"writeBufferSize,omitempty"`
ReadLimit *int64 `toml:"read-limit" json:"readLimit,omitempty"`
Compression *bool `toml:"compression" json:"compression,omitempty"`
Verbose *bool `toml:"verbose" json:"verbose,omitempty"`
}
Options contains settings for the client which have data representations. Each non-nil field is applied when Options is used as an Option.
type URLValue ¶
type URLValue struct {
// contains filtered or unexported fields
}
URLValue provides ergonomics for entering WebSocket URLs as values. When the text looks like a port (e.g. :8080), the URL is interpreted as localhost. When the text looks like a hostname, the prefix ws:// is prepended. The HTTP schemes are converted to their WebSocket counterparts, so that http becomes ws and https becomes wss.
func NewURLValue ¶
NewURLValue creates a new URLValue from a string
func (*URLValue) Reset ¶
func (u *URLValue) Reset()
Reset clears the value, which facilitates its re-use