Documentation ¶
Index ¶
- Constants
- Variables
- func Write(ctx context.Context, c APIConn, api API, id string, payload interface{}) error
- type API
- type APIConn
- type Authenticator
- type Command
- type Conn
- func (ac *Conn) Call(ctx context.Context, api API, payload interface{}) (Command, string, interface{}, error)
- func (ac *Conn) Close() error
- func (ac *Conn) CloseStatus(code websocket.StatusCode, reason string) error
- func (ac *Conn) Connect(ctx context.Context) error
- func (ac *Conn) IsOnline() bool
- func (ac *Conn) Read(ctx context.Context, api API) (Command, string, interface{}, error)
- func (ac *Conn) ReadJSON(ctx context.Context, v any) error
- func (ac *Conn) Write(ctx context.Context, api API, id string, payload interface{}) error
- func (ac *Conn) WriteJSON(ctx context.Context, v any) error
- type Error
- type HandshakeError
- type Header
- type InternalError
- type Message
- type PingRequest
- type PingResponse
- type WSConn
Constants ¶
const ( WSConnectTimeout = 20 * time.Second WSHandshakeTimeout = 15 * time.Second )
const (
StatusHandshakeErr websocket.StatusCode = 4100 // XXX can we just hijack 4100?
)
Variables ¶
var ErrInvalidCommand = errors.New("invalid command")
var PublicKeyAuthError = websocket.CloseError{ Code: StatusHandshakeErr, Reason: HandshakeError("invalid public key").Error(), }
Functions ¶
Types ¶
type APIConn ¶
type APIConn interface { ReadJSON(ctx context.Context, v any) error WriteJSON(ctx context.Context, v any) error }
APIConn provides an API connection.
type Authenticator ¶
type Authenticator interface { HandshakeClient(ctx context.Context, ac APIConn) error HandshakeServer(ctx context.Context, ac APIConn) error }
Authenticator implements authentication between a client and a server.
type Conn ¶
Conn is a client side connection.
func NewConn ¶
func NewConn(urlStr string, authenticator Authenticator) (*Conn, error)
NewConn returns a client side connection object.
func (*Conn) Call ¶
func (ac *Conn) Call(ctx context.Context, api API, payload interface{}) (Command, string, interface{}, error)
Call is a blocking call that returns the command, id and unmarshaled payload.
func (*Conn) CloseStatus ¶
func (ac *Conn) CloseStatus(code websocket.StatusCode, reason string) error
CloseStatus close the connection with the provided StatusCode.
type Error ¶
type Error struct { Timestamp int64 `json:"timestamp"` Trace string `json:"trace,omitempty"` Message string `json:"message"` }
Error is a protocol error type that is used to provide additional error information between a server and client.
A unique "trace" string may be embedded, which can be used to trace errors between a server and client.
func RequestError ¶ added in v0.1.1
RequestError wraps an error to create a protocol request error.
Request errors are usually something caused by a client, e.g. validation or input errors, and therefore should not be logged server-side and do not contain an embedded trace.
func RequestErrorf ¶ added in v0.1.1
RequestErrorf creates a new protocol request error.
Request errors are usually something caused by a client, e.g. validation or input errors, and therefore should not be logged server-side and do not contain an embedded trace.
type HandshakeError ¶
type HandshakeError string
func (HandshakeError) Error ¶
func (he HandshakeError) Error() string
func (HandshakeError) Is ¶
func (he HandshakeError) Is(target error) bool
type Header ¶
type Header struct { Command Command `json:"command"` // Command to execute ID string `json:"id,omitempty"` // Command identifier }
Header prefixes all websocket commands.
type InternalError ¶ added in v0.1.1
type InternalError struct {
// contains filtered or unexported fields
}
InternalError represents an internal application error.
Internal errors are errors that occurred within the application and are not caused by a client (e.g. validation or input errors). The actual error message should not be sent to clients, as it is internal to the application, and may be server-operator specific.
func NewInternalError ¶ added in v0.1.1
func NewInternalError(err error) *InternalError
NewInternalError returns an InternalError wrapping the given error.
func NewInternalErrorf ¶ added in v0.1.1
func NewInternalErrorf(msg string, args ...interface{}) *InternalError
NewInternalErrorf returns an InternalError constructed from the passed message and arguments.
func (InternalError) Error ¶ added in v0.1.1
func (ie InternalError) Error() string
Error satisfies the error interface.
func (InternalError) ProtocolError ¶ added in v0.1.1
func (ie InternalError) ProtocolError() *Error
ProtocolError returns the protocol error representation. This error is intended to be sent to clients.
func (InternalError) Unwrap ¶ added in v0.1.1
func (ie InternalError) Unwrap() error
Unwrap returns the error wrapped by this internal error.
type Message ¶
type Message struct { Header Header `json:"header"` Payload json.RawMessage `json:"payload"` }
Message represents a websocket message.
type PingRequest ¶
type PingRequest struct {
Timestamp int64 `json:"timestamp"` // Local timestamp
}
Ping
type PingResponse ¶
type PingResponse struct { OriginTimestamp int64 `json:"origintimestamp"` // Timestamp from origin Timestamp int64 `json:"timestamp"` // Local timestamp }
PingResponse
type WSConn ¶
type WSConn struct {
// contains filtered or unexported fields
}
func (*WSConn) CloseStatus ¶
func (wsc *WSConn) CloseStatus(code websocket.StatusCode, reason string) error